我想在Springboot中测试我的服务,但是当我的测试到达时,restTemplate.getForObject崩溃了...
代码测试:
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest
@AutoConfigureMockMvc
@ActiveProfiles("test")
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
public class ApplicationTestsCoverServiceImpl {
@Autowired
MonitorServiceImpl monitorService;
@Mock
RestTemplate restTemplate;
@Mock
Mockito mk;
@Test
public void testGetOperativesMonitor() throws Exception {
MonitorOperative operative[] = new MonitorOperative[1];
operative[0] = new MonitorOperative();
operative[0].setOperativeId("1A");
this.mk.when(this.restTemplate.getForObject(
Mockito.anyString(), Mockito.eq(MonitorOperative[].class)))
.thenReturn(operative);
ResponseEntity<List<MonitorOperativeFront>> operative2 = monitorService.getOperativesMonitor("edu");
assertEquals(operative[0].getOperativeId(), "1A");
}
}
我正在为此使用SpringJUnit4ClassRunner。
当Mockito拦截调用restTemplate时,我在实现中创建了一个“可操作”对象。
最后启动monitorService.getOperativesMonitor("edu");
现在我的测试加入了实现
@Service
@Slf4j
public class MonitorServiceImpl implements MonitorService {
@Value("${urlRest.urlBase}")
private String urlBase;
@Autowired
RestTemplate restTemplate;
public ResponseEntity<List<MonitorOperativeFront>> getOperativesMonitor(String userCode) {
log.info(" ---> LogInfo: start ");
String url = this.urlBase + "/" + userCode);
log.info(" ---> LogInfo: call to: " + url);
List<MonitorOperativeFront> list= null ;
try {
MonitorOperative[] operative = this.restTemplate.getForObject(url, MonitorOperative[].class);
list.add(new MonitorOperativeFront(operative[0].getId()));
log.info(" ---> LogInfo: Success ");
} catch (Exception e) {
System.out.println(e.getLocalizedMessage());
System.out.println(e.getMessage());
System.out.println(e.getMessage());
System.out.println(e.getClass());
e.printStackTrace();
}
return new ResponseEntity<>(list, HttpStatus.OK);
}
}
我的代码很简单,我创建了一个url y,然后使用restTemplate,我的应用运行良好,但是当我在Junit中使用“ CoverAs”时,restTemplate中会出现错误:
代码错误:
[INFO ] 2019-08-14 13:19:43.825 [main] ApplicationTests - Started ApplicationTests in 9.325 seconds (JVM running for 44.702)
[WARN ] 2019-08-14 13:19:43.827 [main] LoggingEnvironmentListener - ******************************************************
[WARN ] 2019-08-14 13:19:43.827 [main] LoggingEnvironmentListener - Kafka appenders are not in the configuration, so the system disables sending traces to Kafka
[WARN ] 2019-08-14 13:19:43.827 [main] LoggingEnvironmentListener - ******************************************************
[INFO ] 2019-08-14 13:19:44.436 [main] MonitorServiceImpl - ---> LogInfo: start
[INFO ] 2019-08-14 13:19:44.436 [main] MonitorServiceImpl - ---> LogInfo: call to: http:// secret
No value present
No value present
No value present
class java.util.NoSuchElementException
java.util.NoSuchElementException: No value present
at java.util.Optional.get(Optional.java:135)
at es.bank.darwin.security.authentication.converter.TokenConverter.lambda$getJwtToken$1(TokenConverter.java:55)
at java.util.Optional.orElseGet(Optional.java:267)
at es.bank.darwin.security.authentication.converter.TokenConverter.getJwtToken(TokenConverter.java:55)
at es.bank.darwin.security.authentication.interceptor.JWTInterceptor.intercept(JWTInterceptor.java:35)
at org.springframework.http.client.InterceptingClientHttpRequest$InterceptingRequestExecution.execute(InterceptingClientHttpRequest.java:92)
at es.bank.darwin.omnichannel.interceptor.OmniChannelInterceptor.intercept(OmniChannelInterceptor.java:43)
at org.springframework.http.client.InterceptingClientHttpRequest$InterceptingRequestExecution.execute(InterceptingClientHttpRequest.java:92)
at es.bank.darwin.logging.http.LoggingHeadersInterceptor.intercept(LoggingHeadersInterceptor.java:34)
at org.springframework.http.client.InterceptingClientHttpRequest$InterceptingRequestExecution.execute(InterceptingClientHttpRequest.java:92)
at es.bank.darwin.common.interceptors.AppKeyInterceptor.intercept(AppKeyInterceptor.java:43)
at org.springframework.http.client.InterceptingClientHttpRequest$InterceptingRequestExecution.execute(InterceptingClientHttpRequest.java:92)
at org.springframework.http.client.InterceptingClientHttpRequest.executeInternal(InterceptingClientHttpRequest.java:76)
at org.springframework.http.client.AbstractBufferingClientHttpRequest.executeInternal(AbstractBufferingClientHttpRequest.java:48)
at org.springframework.http.client.AbstractClientHttpRequest.execute(AbstractClientHttpRequest.java:53)
at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:687)
at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:644)
at org.springframework.web.client.RestTemplate.getForObject(RestTemplate.java:296)
at es.bank.mrpa.ServiceImpl.MonitorServiceImpl.getOperativesMonitor(MonitorServiceImpl.java:208)
at es.bank.mrpa.ServiceImpl.MonitorServiceImpl$$FastClassBySpringCGLIB$$e1f6dc2e.invoke(<generated>)
at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204)
at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:746)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:163)
at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:92)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:185)
at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:688)
at es.bank.mrpa.ServiceImpl.MonitorServiceImpl$$EnhancerBySpringCGLIB$$286bbf09.getOperativesMonitor(<generated>)
at es.bank.mrpa.ApplicationTestsCoverServiceImpl.testGetOperativesMonitor(ApplicationTestsCoverServiceImpl.java:52)
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:498)
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.springframework.test.context.junit4.statements.RunBeforeTestExecutionCallbacks.evaluate(RunBeforeTestExecutionCallbacks.java:73)
at org.springframework.test.context.junit4.statements.RunAfterTestExecutionCallbacks.evaluate(RunAfterTestExecutionCallbacks.java:83)
at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:75)
at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:86)
at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:84)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:251)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:97)
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.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:190)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:539)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:761)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:461)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:207)
[INFO ] 2019-08-14 13:19:44.455 [Thread-10] GenericWebApplicationContext - Closing org.springframework.web.context.support.GenericWebApplicationContext@1ecc1a99: startup date [Wed Aug 14 13:19:14 CEST 2019]; parent: org.springframework.context.annotation.AnnotationConfigApplicationContext@729fc36a
[INFO ] 2019-08-14 13:19:44.460 [Thread-10] DefaultLifecycleProcessor - Stopping beans in phase 2147483647
[INFO ] 2019-08-14 13:19:44.461 [Thread-10] DefaultLifecycleProcessor - Stopping beans in phase 2147483547
[INFO ] 2019-08-14 13:19:44.461 [Thread-10] DefaultLifecycleProcessor - Stopping beans in phase 0
[INFO ] 2019-08-14 13:19:44.462 [Thread-18] GenericWebApplicationContext - Closing org.springframework.web.context.support.GenericWebApplicationContext@503b237e: startup date [Wed Aug 14 13:19:35 CEST 2019]; parent: org.springframework.context.annotation.AnnotationConfigApplicationContext@1c33043d
[INFO ] 2019-08-14 13:19:44.501 [Thread-18] DefaultLifecycleProcessor - Stopping beans in phase 2147483647
[INFO ] 2019-08-14 13:19:44.501 [Thread-18] DefaultLifecycleProcessor - Stopping beans in phase 2147483547
[INFO ] 2019-08-14 13:19:44.501 [Thread-18] DefaultLifecycleProcessor - Stopping beans in phase 0
我认为是我的
this.mk.when(this.restTemplate.getForObject(
Mockito.anyString(), Mockito.eq(MonitorOperative[].class)))
.thenReturn(operative);
不运行,因为我的test-Implementation尝试调用restTemplate ...