在模拟中使用两种方法是使用Mockito和Spring MockMVC抛出异常



我正在尝试设置一些简单的MVC单元测试。我要完成的是:

  • 使用Spring MockMVC测试HTTP状态和从控制器返回的HTTP数据。
  • 我的控制器对执行实际逻辑的立面的自动引用。
  • 所以,我创建了一个测试类来测试一个控制器。
  • 我想测试该控制器的两种方法,因此我在测试类中创建了两种方法。
  • 我嘲笑了我的立面并将其注入控制器。
  • 所以,我想通过我正在测试的每种方法的返回来配置此模拟。

我正在使用以下代码:

public class LoginTest {
    MockMvc mockMvc;
    @Mock
    Cadastre facade;
    @InjectMocks
    Login controller;
    @Before
    public void setup() {
        MockitoAnnotations.initMocks(this);
        Mockito.when(facade.login(Mockito.any(String.class))).thenReturn(true);
        Mockito.when(facade.getUser(Mockito.any(String.class))).thenReturn(Mockito.any(UserData.class));
        mockMvc = MockMvcBuilders.standaloneSetup(controller).setMessageConverters(new MappingJackson2HttpMessageConverter()).build();
    }
    @Test
    public void thatLoginUsesStatusOK() throws Exception {
        this.mockMvc.perform(MockMvcRequestBuilders.get(UserFixtures.loginUrl())).andExpect(MockMvcResultMatchers.status().isOk());
    }
    @Test
    public void thatUserDataUsesStatusOK() throws Exception {
        this.mockMvc.perform(MockMvcRequestBuilders.get(UserFixtures.getUserDataUrl())).andExpect(MockMvcResultMatchers.status().isOk());
    }
}

如果我尝试执行此测试类,则失败了错误:

org.mockito.exceptions.misusing.InvalidUseOfMatchersException: 
Invalid use of argument matchers!
1 matchers expected, 2 recorded:
-> at com.example.test.api.login.LoginTest.setup(LoginTest.java:38)
-> at com.example.test.api.login.LoginTest.setup(LoginTest.java:39)
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.example.test.api.login.LoginTest.setup(LoginTest.java:39)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:601)
    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.RunBefores.evaluate(RunBefores.java:24)
    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.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)

如果我将每个模拟的方法行(当行)移入其test()方法时,并将此测试方法分别调用,则它们可工作。但是,如果我尝试执行整个课程,那么只有一个测试方法可以工作,而另一个抛出以下异常(似乎与我上面的原因相同):

org.mockito.exceptions.misusing.InvalidUseOfMatchersException: 
Misplaced argument matcher detected here:
-> at com.example.test.api.login.LoginTest.thatLoginUsesStatusOK(LoginTest.java:45)
You cannot use argument matchers outside of verification or stubbing.
Examples of correct usage of argument matchers:
    when(mock.get(anyInt())).thenReturn(null);
    doThrow(new RuntimeException()).when(mock).someVoidMethod(anyObject());
    verify(mock).someMethod(contains("foo"))
Also, this error might show up because you use argument matchers with methods that cannot be mocked.
Following methods *cannot* be stubbed/verified: final/private/equals()/hashCode().
    at com.example.test.api.login.LoginTest.setup(LoginTest.java:37)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:601)
    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.RunBefores.evaluate(RunBefores.java:24)
    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.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)

所以,我相信原因是我可能不太了解如何使用Mockito。你能把我指向正确的方向吗?

非常感谢!

您的问题是写

thenReturn(Mockito.any(UserData.class))

any方法用于测试过程中出现的匹配值,而不是创建值。您需要指定要返回的UserData对象,大概是通过实例化并将其传递到此处。

相关内容

  • 没有找到相关文章