org.mockito.exceptions.misusing.InvalidUseOfMatchersExceptio



>我有这个测试

@Test
public void addVacancy() throws Exception{
        Vacancy mockVacancy = Mockito.mock(Vacancy.class);
        BindingResult mockResult= Mockito.mock(BindingResult.class);
        RedirectAttributes mockRedirectAttributes=Mockito.mock(RedirectAttributes.class);
        Model mockModel = Mockito.mock(Model.class);
        ModelAndView modelAndView;
        when(mockResult.hasErrors()).thenReturn(false);
        VacancyService vacancyService = Mockito.mock(VacancyService.class);
        vacancyMenuController.vacancyService = vacancyService;
        modelAndView = vacancyMenuController.addVacancy(mockVacancy, mockResult, mockModel, mockRedirectAttributes); 
        Mockito.verify(vacancyService, Mockito.times(1)).add(Mockito.any(Vacancy.class));//good work
        Mockito.verify(mockRedirectAttributes,Mockito.times(1)).addAttribute("message",anyString());// exception
    }

对于此方法:

@RequestMapping("/addVacancy")
    public ModelAndView addVacancy(@ModelAttribute("myVacancy") @Valid Vacancy vacancy,BindingResult result, Model model,RedirectAttributes redirectAttributes){
        if(result.hasErrors()){
            model.addAttribute("message","validation error");
            return new ModelAndView("vacancyDetailsAdd");
        }
        vacancyService.add(vacancy);
        ModelAndView mv = new ModelAndView("redirect:goToVacancyDetails");
        mv.addObject("idVacancy", vacancy.getId());
        redirectAttributes.addAttribute("message", "added correctly at "+ new Date());
        return mv;
    }

为什么这个字符串很好用:

Mockito.verify(vacancyService, Mockito.times(1)).add(Mockito.any(Vacancy.class));//good work

但在这里我看到了例外

Mockito.verify(mockRedirectAttributes,Mockito.times(1)).addAttribute("message",anyString());// exception

完整跟踪:

org.mockito.exceptions.misusing.InvalidUseOfMatchersException: 
Invalid use of argument matchers!
2 matchers expected, 1 recorded:
-> at com.epam.hhsystem.web.controllers.VacancyMenuControllerTest.addVacancy(VacancyMenuControllerTest.java:186)
This exception may occur if matchers are combined with raw values:
    //incorrect:
    someMethod(anyObject(), "raw String");
When using matchers, all arguments have to be provided by matchers.
For example:
    //correct:
    someMethod(anyObject(), eq("String by matcher"));
For more info see javadoc for Matchers class.
    at com.epam.hhsystem.web.controllers.VacancyMenuControllerTest.addVacancy(VacancyMenuControllerTest.java:186)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
    at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
    at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:74)
    at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:83)
    at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:72)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:231)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:88)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)
    at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
    at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:71)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:174)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)

您的addAttribute()验证需要在字符串"消息"周围有eq()。当您使用其中一个匹配器时,您必须将其用于方法中的所有参数。

Mockito.verify(mockRedirectAttributes,Mockito.times(1)).addAttribute(eq("message"),anyString());

请参阅添加的eq

相关内容

  • 没有找到相关文章

最新更新