如何使用mockito模拟休息模板

时间:2015-05-29 09:41:37

标签: java junit mockito resttemplate

我正在为一个具有休息模板调用的类(Test)编写junit。

response = restTemplate.exchange(uri, HttpMethod.POST,
                headersString, SomeObject.class)

我正在使用Mockito写junit。  下面的模拟语句抛出错误

when(restTemplate.exchange(any(URI.class), any(HttpMethod.class),
                headersString , SomeObject.class)).

帮助我如何使用Mockito将其余模板调用放在junit的when语句中。

错误:

java.lang.IllegalArgumentException: 'url' must not be null
    at org.springframework.util.Assert.notNull(Assert.java:112)
    at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:534)
    at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:518)
    at org.springframework.web.client.RestTemplate.exchange(RestTemplate.java:463)
    at (Test.java:80)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:497)
    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.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:76)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
    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.junit.runners.ParentRunner.run(ParentRunner.java:236)
    at org.mockito.internal.runners.JUnit45AndHigherRunnerImpl.run(JUnit45AndHigherRunnerImpl.java:37)
    at org.mockito.runners.MockitoJUnitRunner.run(MockitoJUnitRunner.java:62)
    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:459)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)

org.mockito.exceptions.misusing.InvalidUseOfMatchersException: 
Misplaced argument matcher detected here:
-> at (Test.java:81)

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 org.mockito.internal.runners.util.FrameworkUsageValidator.testFinished(FrameworkUsageValidator.java:25)
    at org.junit.runner.notification.RunNotifier$7.notifyListener(RunNotifier.java:145)
    at org.junit.runner.notification.RunNotifier$SafeNotifier.run(RunNotifier.java:41)
    at org.junit.runner.notification.RunNotifier.fireTestFinished(RunNotifier.java:142)
    at org.junit.internal.runners.model.EachTestNotifier.fireTestFinished(EachTestNotifier.java:37)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:82)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
    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.junit.runners.ParentRunner.run(ParentRunner.java:236)
    at org.mockito.internal.runners.JUnit45AndHigherRunnerImpl.run(JUnit45AndHigherRunnerImpl.java:37)
    at org.mockito.runners.MockitoJUnitRunner.run(MockitoJUnitRunner.java:62)
    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:459)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)

3 个答案:

答案 0 :(得分:3)

正如错误消息所述,您无法混合匹配器和固定值(无匹配器),如下所示:

when(restTemplate.exchange(any(URI.class), any(HttpMethod.class),
     headersString , SomeObject.class)).
//   ^^              ^^

您必须使用eq()匹配器来获取固定值,例如:

when(restTemplate.exchange(any(URI.class), any(HttpMethod.class), 
     eq(headersString), eq(SomeObject.class))).

来自Matchers javadoc

  

警告:

     

如果您使用的是参数匹配器,则所有参数都必须由匹配器提供。

答案 1 :(得分:0)

以下代码有效,

ResponseEntity<String> responseEntity = new ResponseEntity<String>("sampleBodyString", HttpStatus.ACCEPTED);
when(restTemplate.exchange(
 Matchers.anyString(),
 Matchers.any(HttpMethod.class),
 Matchers.<HttpEntity<?>> any(), 
 Matchers.<Class<String>> any())).thenReturn(responseEntity);

答案 2 :(得分:0)

更可靠的解决方案如下所述。我也提到了对我有用的导入声明。下面的代码完美地模拟了其余模板。

import org.mockito.Matchers; 
import static org.mockito.Matchers.any;  
import org.springframework.http.HttpHeaders;  
import org.springframework.http.ResponseEntity;

这是要模拟的实际模板。

    HttpHeaders headers = new Headers();
    headers.setExpires(10000L);     
    ResponseEntity<String> responseEntity = new ResponseEntity<>("dummyString", headers, HttpStatus.OK);
    when(restTemplate.exchange( Matchers.anyString(), 
            Matchers.any(HttpMethod.class),
            Matchers.<HttpEntity<?>> any(), 
            Matchers.<Class<String>> any())).thenReturn(responseEntity);

这里'responseEntity'值不会为空,我们可以使用它来完美地声明一条语句。