Skip to content Skip to sidebar Skip to footer

Need Help To Write A Unit Test Using Mockito And Junit4

Need help to write a unit test for the below code using Mockito and JUnit4, public class MyFragmentPresenterImpl { public Boolean isValid(String value) { return !(Te

Solution 1:

Because of JUnit TestCase class cannot use Android related APIs, we have to Mock it. Use PowerMockito to Mock the static class.

Add two lines above your test case class,

@RunWith(PowerMockRunner.class)
@PrepareForTest(TextUtils.class)
public class YourTest
{

}

And the setup code

@Before
publicvoidsetup() {
    PowerMockito.mockStatic(TextUtils.class);
    PowerMockito.when(TextUtils.isEmpty(any(CharSequence.class))).thenAnswer(new Answer<Boolean>() {
        @Override
        public Boolean answer(InvocationOnMock invocation) throws Throwable {
            CharSequence a = (CharSequence) invocation.getArguments()[0];
            return !(a != null && a.length() > 0);
        }
    });
}

That implement TextUtils.isEmpty() with our own logic.

Also, add dependencies in app.gradle files.

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"

Thanks Behelit's and Exception's answer.

Solution 2:

Use PowerMockito

Add this above your class name, and include any other CUT class names (classes under test)

@RunWith(PowerMockRunner.class)
@PrepareForTest({TextUtils.class})
public class ContactUtilsTest
{

Add this to your @Before

@Before
publicvoidsetup(){
    PowerMockito.mockStatic(TextUtils.class);
    mMyFragmentPresenter=new MyFragmentPresenterImpl();
}

This will make PowerMockito return default values for methods within TextUtils

You would also have to add the relevant gradle depedencies

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"

Solution 3:

This is a known issue as mentioned by @Exception. In my case, I also stumbled upon same situation but on advice of senior dev decided to use Strings.isNullOrEmpty() instead of TextUtils.isEmpty(). It turned out to be a nice way to avoid it.

Update: I should better mention it that this utility function Strings.isNullOrEmpty() requires Guava library.

Solution 4:

This is a known issue, due to a clause in Testing Fundamental of Android which says:

You can use the JUnit TestCase class to do unit testing on a class that does not call Android APIs.

The default behavior is problematic when using classes like Log or TextUtils.

To sum up:

  1. android.jar is mock before, so some Android API return value may not be as expected.
  2. JUnit itself is a single measure for the java code, so try not to use the Android API methods.

Source:http://www.liangfeizc.com/2016/01/28/unit-test-on-android/

Solution 5:

add this line in your gradle file in case of Android Studio.

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

Post a Comment for "Need Help To Write A Unit Test Using Mockito And Junit4"