question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Unit tests with Mockito

See original GitHub issue

Hi, 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:closed
  • Created 6 years ago
  • Comments:12 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
dlemurescommented, Apr 29, 2017

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.

1reaction
stephanenicolascommented, Apr 28, 2017

I just filled an issue on Android Dev tools for this: https://issuetracker.google.com/issues/37781142

Read more comments on GitHub >

github_iconTop Results From Across the Web

Unit tests with Mockito - Tutorial - Vogella.com
Mockito is a popular open source framework for mocking objects in software test. Using Mockito greatly simplifies the development of tests for ...
Read more >
Mockito Tutorial - Tutorialspoint
Mockito is a mocking framework, JAVA-based library that is used for effective unit testing of JAVA applications. Mockito is used to mock interfaces...
Read more >
Clean Unit Tests with Mockito - Reflectoring
Mockito is a framework to mock behavior of components based on values and not to mock values. This means that we better create...
Read more >
A Unit Testing Practitioner's Guide to Everyday Mockito - Toptal
Unit tests are designed to test the behavior of specific classes or methods without relying on the behavior of their dependencies. Since we're...
Read more >
Mockito Tutorial - DigitalOcean
Mockito is a popular mocking framework for Java unit testing. We can easily mock dependencies using Mockito. Mockito coding style is fluent and ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found