๊ฐœ๋ฐœ/Android

[Android] unit test์—์„œ android Util ์‚ฌ์šฉํ•˜๊ธฐ

๋„๋ฆฌ ๐ŸŸ 2021. 4. 15. 22:37

Unit test์—์„œ android Utils๋ฅผ ์‚ฌ์šฉํ•œ ์ฝ”๋“œ๋ฅผ ํ˜ธ์ถœํ•˜๋Š” ๊ฒฝ์šฐ, ์•„๋ž˜์™€ ๊ฐ™์€ ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•œ๋‹ค.

 

์˜ˆ๋ฅผ ๋“ค์–ด, TextUtils.isEmtpy()๋ฅผ ์‚ฌ์šฉํ•œ ์ฝ”๋“œ๋ฅผ ํ…Œ์ŠคํŠธํ•˜๋Š” ๊ฒฝ์šฐ

java.lang.RuntimeException: Method isEmpty in android.text.TextUtils not mocked

๋ฐฉ๋ฒ•1) app์˜ build.gralde์— ์˜ต์…˜ ์ถ”๊ฐ€

android {
  // ...
  testOptions { 
    unitTests.returnDefaultValues = true
  }
}

-> ์ฃผ์˜ : Default๊ฐ’์ด ๋ฐ˜ํ™˜๋˜๊ธฐ ๋•Œ๋ฌธ์—, ์˜ˆ์ƒํ•œ ๋™์ž‘๊ณผ ๋‹ค๋ฅผ ์ˆ˜ ์žˆ๋‹ค.

 

๋ฐฉ๋ฒ•2) src/test/java > android. ํŒจํ‚ค์ง€์— ๋™์ผํ•œ Utils ํด๋ž˜์Šค ์ถ”๊ฐ€ 

์˜ˆ์‹œ) TextUtils๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒฝ์šฐ

ํ”„๋กœ์ ํŠธ์˜ src/test/java/android/text ๊ฒฝ๋กœ์— TextUtils ํด๋ž˜์Šค ์ƒ์„ฑ ๋ฐ ์‚ฌ์šฉํ•œ ๋ฉ”์„œ๋“œ๋ฅผ ๋ณต์‚ฌ&๋ถ™์—ฌ๋„ฃ๊ธฐ

package android.text;

public class TextUtils {
	/**
     * Returns true if the string is null or 0-length.
     * @param str the string to be examined
     * @return true if str is null or zero length
     */
    public static boolean isEmpty(@Nullable CharSequence str) {
        return str == null || str.length() == 0;
    }
}