在模拟中存根两个方法使用Mockito和Spring MockMVC引发异常

我试图建立一些简单的MVC单元测试。我想要完成的是:

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

我正在使用以下代码:

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());

}

}

如果我尝试执行此Test类,它将失败并显示以下错误:

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)

如果我将每个模拟的方法行(Mock.when行)移动到其test()方法内,并分别调用此测试方法,它们将起作用。但是,如果我尝试执行整个类,则只有一个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要返回的对象,大概是实例化一个对象并将其传递到此处。

以上是 在模拟中存根两个方法使用Mockito和Spring MockMVC引发异常 的全部内容, 来源链接: utcz.com/qa/433024.html

回到顶部