Py学习  »  MQ

由于rabbitmq producer导致的junit/mockito错误

user9414660 • 4 年前 • 922 次点击  

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

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

小精灵

@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测试用例: matchserviceinmpltest.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)
Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/46428
 
922 次点击  
文章 [ 1 ]  |  最新文章 4 年前
i.bondarenko
Reply   •   1 楼
i.bondarenko    4 年前

你需要添加 matchRepo.findById(...) expection,否则mock将返回null,并且您的测试在 if (matchObj.isPresent()) 是的。

请尝试以下代码:

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