Why does the void method get a NullPointerException on the mocked object?

I am writing unit tests for my java application that parses on Stash, an atlassian application like Github.

The method I'm testing is as follows:

public List<Message> processEvent(RepositoryRefsChangedEvent event) {
    ArrayList<Message> commitList = new ArrayList<Message>();

    for (RefChange refChange : event.getRefChanges()) {
        LOGGER.info("checking ref change refId={} fromHash={} toHash={} type={}", refChange.getRefId(), refChange.getFromHash(),
                refChange.getToHash(), refChange.getType());

        if (refChange.getRefId().startsWith(REF_BRANCH)) {
            if (refChange.getType() == RefChangeType.ADD && isDeleted(refChange)) {
                LOGGER.info("Deleted a ref that never existed. This shouldn't ever occur.");
            }
            else if (isDeleted(refChange) || isCreated(refChange)) {
                branchCreation(refChange, event.getRepository(), commitList);
            }
            else {
                sepCommits.findCommitInfo(refChange, event.getRepository(), commitList);
            }
        }
        else {
            refNotProcessed(refChange);
        }
    }
    return commitList;
}

I am trying to make sure that if I have git notes commit, processing is ignored and called refNotProcessed(..).

Fortunately, I was able to understand this with relative ease and came to the following solution:

@RunWith (MockitoJUnitRunner.class)
public class RefChangEventTest {
    @Mock RefChange ref;
    @Mock RepositoryRefsChangedEvent refsChangedEvent;
    @Mock Repository repo;
    @Mock ApplicationPropertiesService appService;
    @Mock SEPCommits sepCommits;
    @Spy SEPRefChangeEventImpl sepRefChangeEvent = new SEPRefChangeEventImpl(sepCommits, appService);

    @Before
    public void testSetup() {
        Collection<RefChange> refList = new ArrayList<RefChange>(1);
        refList.add(ref);
        when(refsChangedEvent.getRefChanges()).thenReturn(refList);
        when(refsChangedEvent.getRepository()).thenReturn(repo);
    }

    @Test
    public void gitNotesAreIgnored() throws Exception {
        when(ref.getRefId()).thenReturn("refs/notes/foo");
        when(ref.getFromHash()).thenReturn("da69d7e202d7f66cba01c6f4030bd5975adbf200");
        when(ref.getToHash()).thenReturn("da69d7e202d7f66cba01c6f4030bd5975adbf201");
        doNothing().when(sepCommits).findCommitInfo(any(RefChange.class), any(Repository.class), any(ArrayList.class));

        sepRefChangeEvent.processEvent(refsChangedEvent);
        verify(sepRefChangeEvent, times(1)).refNotProcessed(ref);
    }

After that, I wanted to check if my unit test would end for the right reasons, if I changed the link name to the expected one, for example refs/heads/foo. I would like to see something like:expected 1 execution of refNotProcessed but was not run at all

Instead, I get:

java.lang.NullPointerException
at com.cray.stash.SEPRefChangeEventImpl.processEvent(SEPRefChangeEventImpl.java:62)
at ut.com.isroot.stash.plugin.RefChangEventTest.gitNotesAreIgnored(RefChangEventTest.java:48)
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:47)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
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.runners.ParentRunner.runLeaf(ParentRunner.java:271)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
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:160)
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:253)
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:497)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147)

sepCommits.findCommitInfo(..) void- , . . , , , , sepCommits. NPE?

, :

public SEPRefChangeEventImpl(SEPCommits sepCommits, ApplicationPropertiesService appService) {
    this.sepCommits = sepCommits;

    try {
        endpoint = appService.getPluginProperty("plugin.fedmsg.events.relay.endpoint");
    } catch (Exception e) {
        LOGGER.error("Failed to retrieve properties\n" + e);
    }

    if (endpoint == null) {
        endpoint = "tcp://some.web.address"
    }
}


public void refNotProcessed(RefChange refChange) {
    LOGGER.info("This type of refChange is not supported.\n refId={} fromHash={} toHash={} type={}", refChange.getRefId(), refChange.getFromHash(),
            refChange.getToHash(), refChange.getType());
}

public void findCommitInfo(RefChange ref, Repository repo, ArrayList<Message> commitList) {
    Page<Commit> commits = getChangeset(repo, ref);
    for (Commit commit : commits.getValues()) {
        String topic = topicPrefix + repo.getProject().getKey() + "." + repo.getName() + ".commit";
        Message message = new Message(getInfo(commit, ref), topic);
        commitList.add(message);
    }
}
+4
3

, , sepCommits , processEvent().

, , sepRefChangeEvent. , . . , , , .

0

, Mockito , .
, .

, , , .
: ref name REF_BRANCH, , . , , ( mock) ( , mock ).

       // You do the processing
       if (refChange.getRefId().startsWith(REF_BRANCH)) {
            if (refChange.getType() == RefChangeType.ADD && isDeleted(refChange)) {
                LOGGER.info("Deleted a ref that never existed. This shouldn't ever occur.");
            }
            else if (isDeleted(refChange) || isCreated(refChange)) {
                branchCreation(refChange, event.getRepository(), commitList);
            }
            else {
                sepCommits.findCommitInfo(refChange, event.getRepository(), commitList);
            }
        }
        // You do nothing
        else {
            refNotProcessed(refChange);
        }

, , .

, public method refNotProcessed . :

       // You do the processing
       if (refChange.getRefId().startsWith(REF_BRANCH)) {
            if (refChange.getType() == RefChangeType.ADD && isDeleted(refChange)) {
                LOGGER.info("Deleted a ref that never existed. This shouldn't ever occur.");
            }
            else if (isDeleted(refChange) || isCreated(refChange)) {
                processClass.branchCreation(refChange, event.getRepository(), commitList);
            }
            else {
                sepCommits.findCommitInfo(refChange, event.getRepository(), commitList);
            }
        }
        // You do nothing
        else {
            processClass.refNotProcessed(refChange);
        }
0

:

@RunWith (MockitoJUnitRunner.class)
public class RefChangEventTest {
    @Mock RefChange ref;
    @Mock RepositoryRefsChangedEvent refsChangedEvent;
    @Mock ApplicationPropertiesService appService;
    @Mock ArrayList<Message> mockList;
    @Mock RepositoryService repositoryService;
    @Mock RefService refService;
    @Mock CommitService commitService;
    @Mock SecurityService securityService;
    @Mock Repository repo;

    SEPCommits sepCommits = mock(SEPCommits.class, RETURNS_DEEP_STUBS);
    @Spy SEPRefChangeEventImpl sepRefChangeEvent = new SEPRefChangeEventImpl(sepCommits, appService);

    @Before
    public void testSetup() {
        Collection<RefChange> refList = new ArrayList<RefChange>(1);
        refList.add(ref);
        when(refsChangedEvent.getRefChanges()).thenReturn(refList);
        when(refsChangedEvent.getRepository()).thenReturn(repo);
    }

    @Test
    public void gitNotesAreIgnored() throws Exception {
        when(ref.getRefId()).thenReturn("refs/notes/foo");
        when(ref.getFromHash()).thenReturn("da69d7e202d7f66cba01c6f4030bd5975adbf200");
        when(ref.getToHash()).thenReturn("da69d7e202d7f66cba01c6f4030bd5975adbf201");

        sepRefChangeEvent.processEvent(refsChangedEvent);
        verifyZeroInteractions(sepCommits);
    }

The difference is how sepCommits are being bullied as well RETURN_DEEP_STUBS. The reason I avoid completely initializing sepCommits is because then I need to stun a ton of methods that go inside findCommitInfo(..). I just want to make sure that this method is called or not. I'm not sure if this RETURN_DEEP_STUBSis a good solution, but it worked for me.

0
source

Source: https://habr.com/ru/post/1651303/


All Articles