由于RabbitMq生产商而导致Junit / Mockito错误

时间:2019-10-06 06:00:06

标签: spring-boot rabbitmq junit4

我有一个实现类,它试图保存POST请求,生产者类将从RabbitMQ获取该请求。但是,当我编写Junit测试用例时,它失败了。您能告诉我错误是什么以及如何正确编写吗?

如果需要更多信息,请告诉我。

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测试用例: 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);
    }

如果我按照测试用例的上方注释代码,否则会出现以下错误

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)

1 个答案:

答案 0 :(得分:1)

您需要添加matchRepo.findById(...)期望,否则模拟将返回null,并且您的测试在if (matchObj.isPresent())上失败。

尝试以下代码:

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