Skip to content

Instantly share code, notes, and snippets.

@hanihashemi
Created March 31, 2017 18:02
Show Gist options
  • Save hanihashemi/7ffdbf7e4cb0038ffb81ff9136993cba to your computer and use it in GitHub Desktop.
Save hanihashemi/7ffdbf7e4cb0038ffb81ff9136993cba to your computer and use it in GitHub Desktop.
Mock Android TextUtils
import android.text.TextUtils;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import static org.mockito.Matchers.any;
import static org.mockito.Mockito.when;
import static org.powermock.api.mockito.PowerMockito.mockStatic;
/**
* Created by hani on 3/30/17.
*/
@RunWith(PowerMockRunner.class)
@PrepareForTest(TextUtils.class)
public class SortPointerHelperTest {
@Before
public void mockTextUtils() {
mockStatic(TextUtils.class);
when(TextUtils.isEmpty(any(CharSequence.class))).thenAnswer(new Answer<Boolean>() {
@Override
public Boolean answer(InvocationOnMock invocation) throws Throwable {
CharSequence str = invocation.getArgumentAt(0, CharSequence.class);
return str == null || str.length() == 0;
}
});
}
@Test
public void mytest() {
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment