Need help to write a unit test using Mockito and JUnit4

前端 未结 10 1050
余生分开走
余生分开走 2020-12-15 04:06

Need help to write a unit test for the below code using Mockito and JUnit4,

public class MyFragmentPresenterImpl { 
      public Boolean isValid(String value         


        
10条回答
  •  一向
    一向 (楼主)
    2020-12-15 04:38

    Solution 1:

    I would like to provide a Kotlin and a Java version.

    Kotlin version:

    import android.text.TextUtils
    
    import org.junit.Before
    
    import org.junit.runner.RunWith
    
    import org.mockito.Matchers.any
    
    import org.powermock.api.mockito.PowerMockito
    

    import org.powermock.core.classloader.annotations.PrepareForTest

    import org.powermock.modules.junit4.PowerMockRunner
    
    
    
    @RunWith(PowerMockRunner::class)
    
    @PrepareForTest(TextUtils::class)
    
    class UserOwnedDataTest1 {
    
    
    
        @Before
    
        fun setup() {
    
            PowerMockito.mockStatic(TextUtils::class.java)
    
            PowerMockito.`when`(TextUtils.isEmpty(any(CharSequence::class.java))).thenAnswer { invocation ->
    
                val a = invocation.arguments[0] as? CharSequence
    
               a?.isEmpty() ?: true
    
            }
    
        }
    
    }
    

    Java version:

    import android.text.TextUtils;
    
    
    
    import org.junit.Before;
    
    import org.junit.runner.RunWith;
    
    import org.mockito.stubbing.Answer;
    
    import org.powermock.api.mockito.PowerMockito;
    
    import org.powermock.core.classloader.annotations.PrepareForTest;
    
    import org.powermock.modules.junit4.PowerMockRunner;
    
    
    
    import static org.mockito.Matchers.any;
    
    
    
    @RunWith(PowerMockRunner.class)
    
    @PrepareForTest(TextUtils.class)
    
    public final class UserOwnedDataTest2 {
    
    
    
        @Before
    
        public void setup() {
    
            PowerMockito.mockStatic(TextUtils.class);
    
            PowerMockito.when(TextUtils.isEmpty(any(CharSequence.class))).thenAnswer((Answer) invocation -> {
    
                CharSequence a = (CharSequence) invocation.getArguments()[0];
    
                return !(a != null && a.length() > 0);
    
            });
    
        }
    
    }
    

    Do not forget to add the dependencies:

    testCompile "org.powermock:powermock-module-junit4:1.6.2"
    
    testCompile "org.powermock:powermock-module-junit4-rule:1.6.2"
    
    testCompile "org.powermock:powermock-api-mockito:1.6.2"
    
    testCompile "org.powermock:powermock-classloading-xstream:1.6.2"
    

    I remember that we still need another dependency, but not clearly.

    Anyway you could fix the missing dependency easily.

    Solution 2:

    Or you could add the same package and class name with TextUtils

    package android.text;
    
    
    
    public class TextUtils {
    
        public static boolean isEmpty( CharSequence str) {
    
            return str == null || str.length() == 0;
    
        }
    
    }
    

提交回复
热议问题