Unit tests with Mockito
See original GitHub issueHi, I’m trying to do some unit testing with Toothpick and Mockito, but I have a null pointer exception, when I try to inject the class under test (ContactsPresenter mContactsPresenter).
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.mockito.junit.MockitoJUnitRunner;
import java.util.ArrayList;
import java.util.List;
import javax.inject.Inject;
import io.reactivex.Single;
import toothpick.testing.ToothPickRule;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
@RunWith(MockitoJUnitRunner.class)
public class ContactsPresenterTest {
@Rule public ToothPickRule toothPickRule = new ToothPickRule(this);
@Mock ContactsView mContactsView;
@Mock UserModel mUserModel;
@Mock ContactsDataSource mContactsDataSource;
@Inject ContactsPresenter mContactsPresenter;
@Before
public void setUp() throws Exception {
MockitoAnnotations.initMocks(this);
when(mUserModel.fetchCountries("")).thenReturn(Single.just((List<Country>) new ArrayList<Country>(0)));
toothPickRule.inject(this);
mContactsPresenter.fetchUsers();
}
@Test
public void testUserCalled() {
verify(mContactsView).onUsersListFetching();
}
}
I have these dependencies :
compile 'com.github.stephanenicolas.toothpick:toothpick-runtime:1.0.6'
testCompile 'com.github.stephanenicolas.toothpick:toothpick-testing:1.0.6'
annotationProcessor 'com.github.stephanenicolas.toothpick:toothpick-compiler:1.0.6'
testCompile "org.mockito:mockito-core:2.8.9"
This is the generated factory :
import java.lang.Override;
import toothpick.Factory;
import toothpick.MemberInjector;
import toothpick.Scope;
public final class ContactsPresenter$$Factory implements Factory<ContactsPresenter> {
private MemberInjector<ContactsPresenter> memberInjector = new ....ContactsPresenter$$MemberInjector();
@Override
public ContactsPresenter createInstance(Scope scope) {
scope = getTargetScope(scope);
ContactsPresenter contactsPresenter = new ContactsPresenter();
memberInjector.inject(contactsPresenter, scope);
return contactsPresenter;
}
@Override
public Scope getTargetScope(Scope scope) {
return scope;
}
@Override
public boolean hasScopeAnnotation() {
return false;
}
@Override
public boolean hasProvidesSingletonInScopeAnnotation() {
return false;
}
}
java.lang.NullPointerException at …ContactsPresenterTest.setUp(ContactsPresenterTest.java:47) 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.RunBefores.evaluate(RunBefores.java:24) at toothpick.testing.ToothPickStatement.evaluate(ToothPickStatement.java:17) 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.DefaultInternalRunner$1.run(DefaultInternalRunner.java:78) at org.mockito.internal.runners.DefaultInternalRunner.run(DefaultInternalRunner.java:84) at org.mockito.internal.runners.StrictRunner.run(StrictRunner.java:39) at org.mockito.junit.MockitoJUnitRunner.run(MockitoJUnitRunner.java:161) 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)
Issue Analytics
- State:
- Created 6 years ago
- Comments:12 (4 by maintainers)
Top GitHub Comments
Hi @fabiogrum .
Glad it is working.
Regarding the scope, in TP we always need a scope to make the injections. So you should provide an scope name inside the rule.
I just filled an issue on Android Dev tools for this: https://issuetracker.google.com/issues/37781142