RxJava: Scheduler is null while trying to unit test an observable - android

I'm trying to unit test an observable in a viewmodel but I'm getting the following error. I have included the observable in question...
getRepository()
.executeGetAccount()
.doOnSubscribe(response -> liveData.postValue(Resource.loading()))
.subscribeOn(getSchedulerProvider().io())
.observeOn(getSchedulerProvider().ui())
Here's my test class
#Config(sdk = {Build.VERSION_CODES.O_MR1})
#RunWith(RobolectricTestRunner.class)
#PowerMockIgnore({ "org.robolectric.*", "android.*", "javax.net.ssl.*", "androidx.*" })
public class DashboardActivityViewModelTest {
#Rule
public PowerMockRule powerMockRule = new PowerMockRule();
private DashboardActivityViewModel viewModel;
String result = "";
AppSchedulerProvider sProvider;
AppPreferencesHelper appPreferencesHelper;
#Before
public void init(){
sProvider = PowerMockito.mock(AppSchedulerProvider.class);
appPreferencesHelper = PowerMockito.mock(AppPreferencesHelper.class);
RetrofitTestHelper retrofitTestHelper = new RetrofitTestHelper(appPreferencesHelper, sProvider);
viewModel = new DashboardActivityViewModel(retrofitTestHelper.provideRepository(), sProvider);
}
#Test
public void testRepositoryInit(){
assertNotNull(sProvider);
assertNotNull(appPreferencesHelper);
assertNotNull(viewModel);
}
#Test
public void testGetAccount(){
viewModel.executeGetAccount();
assertNotNull(viewModel.getAccountsWithBalanceLiveData().getValue());
}
}
Here the error log:
java.lang.NullPointerException: scheduler is null
at io.reactivex.internal.functions.ObjectHelper.requireNonNull(ObjectHelper.java:39)
at io.reactivex.Observable.subscribeOn(Observable.java:12343)
at com.example.xxxx.xxx.dashboard.DashboardActivityViewModel.executeGetAccount(DashboardActivityViewModel.java:384)
at com.example.xxxx.xxx.DashboardActivityViewModelTest.testGetAccount(DashboardActivityViewModelTest.java:88)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
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.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
at org.powermock.modules.junit4.rule.PowerMockStatement$1.run(PowerMockRule.java:83)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.powermock.reflect.internal.WhiteboxImpl.performMethodInvocation(WhiteboxImpl.java:1819)
at org.powermock.reflect.internal.WhiteboxImpl.doInvokeMethod(WhiteboxImpl.java:801)
at org.powermock.reflect.internal.WhiteboxImpl.invokeMethod(WhiteboxImpl.java:666)
at org.powermock.reflect.Whitebox.invokeMethod(Whitebox.java:401)
at org.powermock.classloading.AbstractClassloaderExecutor.getResult(AbstractClassloaderExecutor.java:69)
at org.powermock.classloading.AbstractClassloaderExecutor.executeWithClassLoader(AbstractClassloaderExecutor.java:59)
at org.powermock.classloading.SingleClassloaderExecutor.execute(SingleClassloaderExecutor.java:67)
at org.powermock.classloading.AbstractClassloaderExecutor.execute(AbstractClassloaderExecutor.java:43)
at org.powermock.modules.junit4.rule.PowerMockStatement.evaluate(PowerMockRule.java:75)
at org.robolectric.RobolectricTestRunner$HelperTestRunner$1.evaluate(RobolectricTestRunner.java:546)
at org.robolectric.internal.SandboxTestRunner$2.lambda$evaluate$0(SandboxTestRunner.java:252)
at org.robolectric.internal.bytecode.Sandbox.lambda$runOnMainThread$0(Sandbox.java:89)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at java.lang.Thread.run(Thread.java:748)

Related

Android Studio Unit tests are failing

I am facing a problem while running my unit test on android studio.
If I run a unit test by right-clicking on the file and select run unit test option then unit test runs successfully, but if I use the command line option then my unit test are failing.
the command I am using ./gradlew testDebugUnitTest
the error I am getting is
Could not initialize plugin: interface org.mockito.plugins.MockMaker (alternate: null)
java.lang.IllegalStateException: Could not initialize plugin: interface org.mockito.plugins.MockMaker (alternate: null)
at org.mockito.internal.configuration.plugins.PluginLoader$1.invoke(PluginLoader.java:74)
at com.sun.proxy.$Proxy18.isTypeMockable(Unknown Source)
at org.mockito.internal.util.MockUtil.typeMockabilityOf(MockUtil.java:29)
at org.mockito.internal.util.MockCreationValidator.validateType(MockCreationValidator.java:22)
at org.mockito.internal.creation.MockSettingsImpl.validatedSettings(MockSettingsImpl.java:240)
at org.mockito.internal.creation.MockSettingsImpl.build(MockSettingsImpl.java:228)
at org.mockito.internal.MockitoCore.mock(MockitoCore.java:61)
at org.mockito.Mockito.mock(Mockito.java:1908)
at com.walmart.rxbaggingimpl.businesslogics.AddBoxBLTest.<init>(AddBoxBLTest.kt:50)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.createTestInstance(PowerMockJUnit44RunnerDelegateImpl.java:197)
at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.createTest(PowerMockJUnit44RunnerDelegateImpl.java:182)
at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.invokeTestMethod(PowerMockJUnit44RunnerDelegateImpl.java:204)
at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.runMethods(PowerMockJUnit44RunnerDelegateImpl.java:160)
at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$1.run(PowerMockJUnit44RunnerDelegateImpl.java:134)
at org.junit.internal.runners.ClassRoadie.runUnprotected(ClassRoadie.java:34)
at org.junit.internal.runners.ClassRoadie.runProtected(ClassRoadie.java:44)
at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.run(PowerMockJUnit44RunnerDelegateImpl.java:136)
at org.powermock.modules.junit4.common.internal.impl.JUnit4TestSuiteChunkerImpl.run(JUnit4TestSuiteChunkerImpl.java:117)
at org.powermock.modules.junit4.common.internal.impl.AbstractCommonPowerMockRunner.run(AbstractCommonPowerMockRunner.java:57)
at org.powermock.modules.junit4.PowerMockRunner.run(PowerMockRunner.java:59)
at org.gradle.api.internal.tasks.testing.junit.JUnitTestClassExecutor.runTestClass(JUnitTestClassExecutor.java:110)
at org.gradle.api.internal.tasks.testing.junit.JUnitTestClassExecutor.execute(JUnitTestClassExecutor.java:58)
at org.gradle.api.internal.tasks.testing.junit.JUnitTestClassExecutor.execute(JUnitTestClassExecutor.java:38)
at org.gradle.api.internal.tasks.testing.junit.AbstractJUnitTestClassProcessor.processTestClass(AbstractJUnitTestClassProcessor.java:62)
at org.gradle.api.internal.tasks.testing.SuiteTestClassProcessor.processTestClass(SuiteTestClassProcessor.java:51)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.gradle.internal.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:35)
at org.gradle.internal.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:24)
at org.gradle.internal.dispatch.ContextClassLoaderDispatch.dispatch(ContextClassLoaderDispatch.java:32)
at org.gradle.internal.dispatch.ProxyDispatchAdapter$DispatchingInvocationHandler.invoke(ProxyDispatchAdapter.java:93)
at com.sun.proxy.$Proxy2.processTestClass(Unknown Source)
at org.gradle.api.internal.tasks.testing.worker.TestWorker.processTestClass(TestWorker.java:118)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.gradle.internal.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:35)
at org.gradle.internal.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:24)
at org.gradle.internal.remote.internal.hub.MessageHubBackedObjectConnection$DispatchWrapper.dispatch(MessageHubBackedObjectConnection.java:175)
at org.gradle.internal.remote.internal.hub.MessageHubBackedObjectConnection$DispatchWrapper.dispatch(MessageHubBackedObjectConnection.java:157)
at org.gradle.internal.remote.internal.hub.MessageHub$Handler.run(MessageHub.java:404)
at org.gradle.internal.concurrent.ExecutorPolicy$CatchAndRecordFailures.onExecute(ExecutorPolicy.java:63)
at org.gradle.internal.concurrent.ManagedExecutorImpl$1.run(ManagedExecutorImpl.java:46)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at org.gradle.internal.concurrent.ThreadFactoryImpl$ManagedThreadRunnable.run(ThreadFactoryImpl.java:55)
at java.lang.Thread.run(Thread.java:748)
Caused by: java.lang.IllegalStateException: Failed to load interface org.mockito.plugins.MockMaker implementation declared in sun.misc.CompoundEnumeration#7f432e48
at org.mockito.internal.configuration.plugins.PluginInitializer.loadImpl(PluginInitializer.java:54)
at org.mockito.internal.configuration.plugins.PluginLoader.loadPlugin(PluginLoader.java:57)
at org.mockito.internal.configuration.plugins.PluginLoader.loadPlugin(PluginLoader.java:44)
at org.mockito.internal.configuration.plugins.PluginRegistry.<init>(PluginRegistry.java:22)
at org.mockito.internal.configuration.plugins.Plugins.<clinit>(Plugins.java:19)
at org.mockito.internal.util.MockUtil.<clinit>(MockUtil.java:24)
... 51 more
Caused by: java.lang.ClassCastException: class com.sun.tools.javac.api.JavacTool
at java.lang.Class.asSubclass(Class.java:3404)
at javax.tools.ToolProvider.getSystemToolClass(ToolProvider.java:178)
at javax.tools.ToolProvider.getSystemTool(ToolProvider.java:158)
at javax.tools.ToolProvider.getSystemJavaCompiler(ToolProvider.java:102)
at org.mockito.internal.creation.bytebuddy.InlineByteBuddyMockMaker.<init>(InlineByteBuddyMockMaker.java:170)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
at java.lang.Class.newInstance(Class.java:442)
at org.mockito.internal.configuration.plugins.PluginInitializer.loadImpl(PluginInitializer.java:49)
... 56 more
code is
class MYTest: BaseTest() {
private val api = APISERVICE(APIServiceClientMock)
private val logapiServices = LogServices(APIServiceClientMock)
private val myBL= MYBL(api, logspiServices, MockEventTrackLogger)
fun getMockResponse(): StopViewResponse {
val expectedResponse = StopViewResponse()
expectedResponse.responseCode = Constants.SUCCESS_CODE
expectedResponse.responseTexts = listOf("Success")
expectedResponse setMockResponseAs SUCCESS
return expectedResponse
}
#Test
fun test1() {
runBlocking {
val data = MyModel()
data.id = 1
data.orderId = 1
getMockResponse()
rxPackBL.completeView(data)
Assert.assertTrue(myBL.getCompleteEvent().value?.peekContent() is Success<*>)
Utils.data= data
myBL.completeView()
Assert.assertTrue(myBL.getCompleteEvent().value?.peekContent() is Success<*>)
}
}
Any help will be appreciated
Thanks

VerifyError on logical operations using Proguard

I'm using Proguard on an android library but it seems to create a VerifyError on one specific class while shrinking or obfuscating the sample code. (i.e. using -dontobfuscate and -dontshrink will remove the error).
I could isolate the error on a GitHub repository with a simple example (The command ./gradlew clean test should reproduce the error)
but I don't really understand the real problem here.
My failing test will only try to instantiate an object:
package a.b.c;
public class FailingTest {
#org.junit.Test
public void name() throws Exception {
new D();
}
}
And the associated class is as simple as that:
package a.b.c;
public class D {
public void f(int g) {
if (g < 0) {
System.out.println("test");
}
}
}
in fact any logical operation will provoke an error (e.g. only putting System.out.println(g == 2); in the method body will fail as well).
Proguard is configured as follow:
-keep public class a.b.c.D {
public protected *;
}
# With those options, the test will be successful.
#-dontobfuscate
#-dontshrink
The stacktrace is as follow:
java.lang.VerifyError: Expecting a stackmap frame at branch target 37
Exception Details:
Location:
a/b/c/D.f(I)V #17: ifeq
Reason:
Expected stackmap frame at this location.
Bytecode:
0x0000000: bb00 0559 b700 0ab6 000b 4d2c b900 0c01
0x0000010: 0099 0014 2cb9 000d 0100 4eb2 0007 2db6
0x0000020: 0008 a7ff e9b1
at a.b.c.FailingTest.name(FailingTest.java:7)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
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.InvokeMethod.evaluate(InvokeMethod.java:17)
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.gradle.api.internal.tasks.testing.junit.JUnitTestClassExecuter.runTestClass(JUnitTestClassExecuter.java:114)
at org.gradle.api.internal.tasks.testing.junit.JUnitTestClassExecuter.execute(JUnitTestClassExecuter.java:57)
at org.gradle.api.internal.tasks.testing.junit.JUnitTestClassProcessor.processTestClass(JUnitTestClassProcessor.java:66)
at org.gradle.api.internal.tasks.testing.SuiteTestClassProcessor.processTestClass(SuiteTestClassProcessor.java:51)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at org.gradle.internal.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:35)
at org.gradle.internal.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:24)
at org.gradle.internal.dispatch.ContextClassLoaderDispatch.dispatch(ContextClassLoaderDispatch.java:32)
at org.gradle.internal.dispatch.ProxyDispatchAdapter$DispatchingInvocationHandler.invoke(ProxyDispatchAdapter.java:93)
at com.sun.proxy.$Proxy3.processTestClass(Unknown Source)
at org.gradle.api.internal.tasks.testing.worker.TestWorker.processTestClass(TestWorker.java:109)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at org.gradle.internal.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:35)
at org.gradle.internal.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:24)
at org.gradle.internal.remote.internal.hub.MessageHub$Handler.run(MessageHub.java:377)
at org.gradle.internal.concurrent.ExecutorPolicy$CatchAndRecordFailures.onExecute(ExecutorPolicy.java:54)
at org.gradle.internal.concurrent.StoppableExecutorImpl$1.run(StoppableExecutorImpl.java:40)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)
Updating the buildscript's dependency com.android.tools.build:gradle from 2.3.3 to 3.0.0 and gradle wrapper from 3.3 to 4.1 seems to fix Proguard's behaviour.
I guess it's a bug inherent to the Proguard version embedded in this specific dependency.

Error while doing unit test - "Actually, there were zero interactions with this mock" - Android

I am trying to unit test my presenter and view in android app with the following code :
#RunWith(MockitoJUnitRunner.class)
public class MovieDetailsPresenterUnitTest {
#Mock
NetworkAPI mNetworkAPI;
#Mock
NetworkService mNetworkService;
#Mock
MovieDetailsContract.View mView;
#Mock
MovieDetailsActivity mMovieDetailsActivity;
private MovieDetailsPresenter mMovieDetailsPresenter;
#Before
public void setUp() throws Exception {
this.mMovieDetailsPresenter = new MovieDetailsPresenter(mNetworkService, mMovieDetailsActivity);
}
#Rule
public final RxSchedulersOverrideRule mOverrideSchedulersRule = new RxSchedulersOverrideRule();
#Test
public void testMovieDetailsEmptyResponse() throws Exception {
MovieDetails movieDetails = new MovieDetails();
when(mNetworkAPI.getMovieDetails("abcd")).thenReturn(Observable.<MovieDetails>just(movieDetails));
mMovieDetailsPresenter.loadMovieDetails("abcd");
verify(mMovieDetailsActivity).onSupportNavigateUp();
}
#After
public void tearDown() throws Exception {
RxAndroidPlugins.getInstance().reset();
}
}
But I get this error :
Wanted but not invoked:
mMovieDetailsActivity.onSupportNavigateUp();
-> at com.android.sushil.omdbclient.MovieDetailsPresenterUnitTest.testMovieDetailsEmptyResponse(MovieDetailsPresenterUnitTest.java:65)
Actually, there were zero interactions with this mock.
Wanted but not invoked:
mMovieDetailsActivity.onSupportNavigateUp();
-> at com.android.sushil.omdbclient.MovieDetailsPresenterUnitTest.testMovieDetailsEmptyResponse(MovieDetailsPresenterUnitTest.java:65)
Actually, there were zero interactions with this mock.
at com.android.sushil.omdbclient.MovieDetailsPresenterUnitTest.testMovieDetailsEmptyResponse(MovieDetailsPresenterUnitTest.java:65)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
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.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
at com.android.sushil.omdbclient.util.RxSchedulersOverrideRule$3.evaluate(RxSchedulersOverrideRule.java:45)
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.mockito.internal.runners.JUnit45AndHigherRunnerImpl.run(JUnit45AndHigherRunnerImpl.java:37)
at org.mockito.runners.MockitoJUnitRunner.run(MockitoJUnitRunner.java:62)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:117)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:42)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:262)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:84)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147)
Your mocks mNetworkAPI and mView are never injected anywhere and hence the stubbing
when(mNetworkAPI.getMovieDetails("abcd")).thenReturn(Observable.<MovieDetails>just(movieDetails));
will never happen. Since I can't see the source code for MovieDetailsPresenter I am just guessing that this lack of stubbing leads to the logic of the onSupportNavigateUp() method never being called. You need to make sure all your mocks are included in mMovieDetailsPresenter by marking it with #InjectMocks.
#InjectMocks
private MovieDetailsPresenter mMovieDetailsPresenter;
Although I can't be 100% sure about this until we see the code for more of your classes.

Android test project can't run in Android studio

I try to run this project on Android studio 2.3
public class ImplicitTest extends ActivityInstrumentationTestCase2<ActivityLoaderActivity> {
private Solo solo;
public ImplicitTest() {
super(ActivityLoaderActivity.class);
}
public void setUp() throws Exception {
solo = new Solo(getInstrumentation(),getActivity());
}
#Override
public void tearDown() throws Exception {
solo.finishOpenedActivities();
}
// Executes the ImplicitTest
public void testRun() {
int delay = 2000;
// =================== Section One =====================
// Wait for activity: 'course.labs.intentslab.ActivityLoaderActivity'
assertTrue(
"ImplicitTest:" +
"Section One:" +
"ActivityLoaderActivity did not load correctly", solo.waitForActivity(ActivityLoaderActivity.class,delay));
solo.sleep(delay);
// Click on Implicit Activation Button
solo.clickOnView(solo
.getView(R.id.implicit_activation_button));
// Wait for activity: 'com.android.internal.app.ChooserActivity'
assertTrue(
"ImplicitTest:" +
"Section One:" +
"ChooserActivity was not launched correctly",
solo.waitForActivity("ChooserActivity",delay));
solo.sleep(delay);
assertTrue(
"ImplicitTest:" +
"Section One:" +
"MyBrowser was not found",
solo.searchText("MyBrowser", true));
}
}
But i get this Exception:
java.lang.NullPointerException
at com.robotium.solo.ActivityUtils.setupActivityMonitor(ActivityUtils.java:107)
at com.robotium.solo.ActivityUtils.<init>(ActivityUtils.java:59)
at com.robotium.solo.Solo.<init>(Solo.java:137)
at com.robotium.solo.Solo.<init>(Solo.java:96)
at course.labs.intentslab.ImplicitTest.setUp(ImplicitTest.java:18)
at junit.framework.TestCase.runBare(TestCase.java:139)
at junit.framework.TestResult$1.protect(TestResult.java:122)
at junit.framework.TestResult.runProtected(TestResult.java:142)
at junit.framework.TestResult.run(TestResult.java:125)
at junit.framework.TestCase.run(TestCase.java:129)
at junit.framework.TestSuite.runTest(TestSuite.java:252)
at junit.framework.TestSuite.run(TestSuite.java:247)
at org.junit.internal.runners.JUnit38ClassRunner.run(JUnit38ClassRunner.java:86)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:117)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:42)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:262)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:84)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147)
java.lang.ExceptionInInitializerError
at com.robotium.solo.Solo.<init>(Solo.java:138)
at com.robotium.solo.Solo.<init>(Solo.java:96)
at course.labs.intentslab.ImplicitTest.setUp(ImplicitTest.java:18)
at junit.framework.TestCase.runBare(TestCase.java:139)
at junit.framework.TestResult$1.protect(TestResult.java:122)
at junit.framework.TestResult.runProtected(TestResult.java:142)
at junit.framework.TestResult.run(TestResult.java:125)
at junit.framework.TestCase.run(TestCase.java:129)
at junit.framework.TestSuite.runTest(TestSuite.java:252)
at junit.framework.TestSuite.run(TestSuite.java:247)
at org.junit.internal.runners.JUnit38ClassRunner.run(JUnit38ClassRunner.java:86)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:117)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:42)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:262)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:84)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147)
Caused by: java.lang.RuntimeException: java.lang.ClassNotFoundException: android.view.WindowManagerImpl
at com.robotium.solo.ViewFetcher.<clinit>(ViewFetcher.java:441)
... 21 more
Caused by: java.lang.ClassNotFoundException: android.view.WindowManagerImpl
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:264)
at com.robotium.solo.ViewFetcher.<clinit>(ViewFetcher.java:438)
... 21 more
Also I add this line in build.gradle but it doesn't work:
androidTestCompile 'com.jayway.android.robotium:robotium:5.3.1'
testCompile 'junit:junit:4.12'
compile 'com.jayway.android.robotium:robotium-solo:5.3.1'

Cannot instantiate OrmLite helper in a unit test class in Android Studio

This is my code:
#RunWith(MockitoJUnitRunner.class)
public class OrmLiteTest {
OrmLiteDatabaseHelper databaseHelper;
#Mock
Context mMockContext;
#Before
public void setup() {
mMockContext = Mockito.mock(Context.class);
databaseHelper = OpenHelperManager.getHelper(mMockContext,
OrmLiteDatabaseHelper.class);
}
I get the following exception when I run the test:
java.lang.IllegalStateException: Could not construct instance of helper class class com.example.myapp.application.OrmLiteDatabaseHelper
at com.j256.ormlite.android.apptools.OpenHelperManager.constructHelper(OpenHelperManager.java:222)
at com.j256.ormlite.android.apptools.OpenHelperManager.loadHelper(OpenHelperManager.java:170)
at com.j256.ormlite.android.apptools.OpenHelperManager.getHelper(OpenHelperManager.java:78)
at com.bulatsa.despark.OrmLiteTest.setup(OrmLiteTest.java:39)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
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.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.mockito.internal.runners.JUnit45AndHigherRunnerImpl.run(JUnit45AndHigherRunnerImpl.java:37)
at org.mockito.runners.MockitoJUnitRunner.run(MockitoJUnitRunner.java:62)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:78)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:212)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:68)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:140)
Caused by: java.lang.reflect.InvocationTargetException
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:526)
at com.j256.ormlite.android.apptools.OpenHelperManager.constructHelper(OpenHelperManager.java:220)
... 31 more
Caused by: java.lang.NullPointerException
at com.j256.ormlite.android.apptools.OrmLiteSqliteOpenHelper.openFileId(OrmLiteSqliteOpenHelper.java:310)
at com.j256.ormlite.android.apptools.OrmLiteSqliteOpenHelper.<init>(OrmLiteSqliteOpenHelper.java:76)
at com.bulatsa.despark.application.OrmLiteDatabaseHelper.<init>(OrmLiteDatabaseHelper.java:28)
... 36 more
You are trying to invoke a method (or similar) on an object which is null in the OrmLiteSqliteOpenHelper.java, line 310
As you didn't share that code, that's all we can say...
Caused by: java.lang.NullPointerException
at com.j256.ormlite.android.apptools.OrmLiteSqliteOpenHelper.openFileId(OrmLiteSqliteOpenHelper.java:310)
Oh, and the line...
mMockContext = Mockito.mock(Context.class);
...should be removed, since the #RunWith(MockitoJUnitRunner.class) will already inject a mock there, no need to create one manually.

Categories

Resources