Search code examples
spring-bootrabbitmqjunit4

Junit / Mockito error due to RabbitMq producer


I have a implementation class which is trying to save the POST request, which will be taken by producer class from RabbitMQ. But when I write the Junit test case, it is failing. Can you let me know what is the error and how to write it correctly ?

Please let me know, if any more information is required.

MatchServiceImpl.java

@Override
    public boolean saveMatch(Match match) throws MatchAlreadyExistsException {
        MatchDTO matchDto = new MatchDTO();
        matchDto.setUnique_id(match.getUnique_id());
        matchDto.setTeamOne(match.getTeamOne());
        matchDto.setTeamTwo(match.getTeamTwo());
        matchDto.setMatchDate(match.getMatchDate());
        matchDto.setMatchStarted(match.isMatchStarted());
        matchDto.setUserId(match.getUserId());

        final Optional<Match> matchObj = matchRepository.findById(match.getId());
        if (matchObj.isPresent()) {
            throw new MatchAlreadyExistsException("Could not save match. Match is already exists");
        }
        matchRepository.save(match);
       //producer.sendMessageToRabbitMq(matchDto);
        return true;
    }

Junit Test Case: MatchServiceImplTest.java

@Test
    public void testSavematchSuccess() throws MatchAlreadyExistsException {
        when(matchRepo.save(match)).thenReturn(match);
        final boolean flag = matchServiceImpl.saveMatch(match);
        assertTrue("saving match failed", flag);
        verify(matchRepo, times(1)).save(match);
    }

If I comment the code as in above the test case pass, else I get the following error

java.lang.NullPointerException
    at com.stackroute.favouriteservice.service.MatchServiceImpl.saveMatch(MatchServiceImpl.java:44)
    at com.stackroute.favouriteservice.service.MatchServiceImplTest.testSavematchSuccess(MatchServiceImplTest.java:53)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:566)
    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.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.runner.JUnitCore.run(JUnitCore.java:137)
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
    at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
    at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
    at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)

Solution

  • You need add matchRepo.findById(...) expectation, otherwise mock will return null and your test fails on if (matchObj.isPresent()).

    Try following code:

    when(matchRepo.findById(match.getId())).thenReturn(Optional.empty());
    when(matchRepo.save(match)).thenReturn(match);