I can't initialize MockWebServer on androidTest folder. I've already added this line on my gradle
androidTestCompile('com.squareup.okhttp3:mockwebserver:3.6.0') {
exclude group: 'com.squareup.okio', module: 'okio'
exclude group: 'com.squareup.okhttp3', module: 'okhttp'
}
.
I tried to follow the implementation from https://github.com/riggaroo/android-retrofit-test-examples. I've cloned the project, change the version of MockWebServer used by this example to the latest one and it still works fine but the implementation from my project still gives me same error.
java.lang.NoSuchMethodError: No static method initializeInstanceForTests()V in class Ld/a/a; or its super classes (declaration of 'd.a.a' appears in /data/app/com.agri.fma-2/base.apk)
at okhttp3.mockwebserver.MockWebServer.<clinit>(MockWebServer.java:101)
at com.agri.fma.activity.TestInputTenantActivity.setUp(TestInputTenantActivity.java:68)
at java.lang.reflect.Method.invoke(Native Method)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:24)
at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
at android.support.test.internal.statement.UiThreadStatement.evaluate(UiThreadStatement.java:55)
at android.support.test.rule.ActivityTestRule$ActivityStatement.evaluate(ActivityTestRule.java:270)
at org.junit.rules.RunRules.evaluate(RunRules.java:20)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runners.Suite.runChild(Suite.java:128)
at org.junit.runners.Suite.runChild(Suite.java:27)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at org.junit.runner.JUnitCore.run(JUnitCore.java:115)
at android.support.test.internal.runner.TestExecutor.execute(TestExecutor.java:59)
at android.support.test.runner.AndroidJUnitRunner.onStart(AndroidJUnitRunner.java:262)
at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1886)
Below, TestInputTenantActivity code
public class TestInputTenantActivity {
@Rule public IntentsTestRule<InputTenantActivity> activityTestRule =
new IntentsTestRule<>(InputTenantActivity.class, false, false);
@Inject InputTenantView view;
@Mock
SharedPreferenceManager sharedPreferenceManager;
private TestMainApplication app;
private MockWebServer mockWebServer;
@Before
public void setUp() throws Exception {
app = (TestMainApplication) InstrumentationRegistry.getInstrumentation()
.getTargetContext().getApplicationContext();
DaggerTestInputTenantComponent.builder().testAppComponent(app.getAppComponent())
.testInputTenantModule(new TestInputTenantModule())
.build().inject(this);
mockWebServer = new MockWebServer();
mockWebServer.start();
}
@Test
public void testEmptyTenant(){
activityTestRule.launchActivity(new Intent());
onView(withId(R.id.tenant)).perform(typeText(""));
onView(withId(R.id.action_next)).perform(click());
onView(withId(R.id.tenant)).check(matches(hasErrorText(app.getString(R.string.empty_tenant))));
intended((hasComponent(new ComponentName(getTargetContext(), LoginActivity.class))), times(0));
}
@Test
public void testTenantWithWhiteSpace(){
String errorMessage = String.format(
Locale.US,
app.getString(R.string.invalid_string_error),
"Tenant name");
activityTestRule.launchActivity(new Intent());
onView(withId(R.id.tenant)).perform(typeText(TENANT_WITH_WHITESPACE));
onView(withId(R.id.action_next)).perform(click());
onView(withId(R.id.tenant)).check(matches(hasErrorText(errorMessage)));
intended((hasComponent(new ComponentName(getTargetContext(), LoginActivity.class))), times(0));
}
@Test
public void testValidTenant(){
activityTestRule.launchActivity(new Intent());
onView(withId(R.id.tenant)).perform(typeText(VALID_TENANT));
onView(withId(R.id.action_next)).perform(click());
intended(hasComponent(new ComponentName(getTargetContext(), LoginActivity.class)));
}
@After
public void tearDown() throws Exception {
mockWebServer.shutdown();
}
}