我能够运行一个Rest Controller PUT方法,该方法通过Spring Boot Application使用预计的自动更新@Service。在尝试执行Spring JUnit测试时,相同的自动配线失败。我曾尝试阅读多个线程与类似的问题。我确保我没有通过“新”关键字创建@服务,我尝试了上下文配置和其他方法...但是一切似乎都是徒劳的。我不确定我哪里出错了。
我的Spring Boot应用程序类-
@SpringBootApplication
@ComponentScan({
"com.initech.myapp.*"
})
public class IngestionServerApplication {
private static final Log logger = LogFactory.getLog(IngestionServerApplication.class);
public static void main(String[] args) {
SpringApplication.run(IngestionServerApplication.class, args);
logger.info("Ingestion Server Application started...");
}
}
Rest控制器类-
package com.initech.myapp.ingestion.controller;
@RestController
public class IngestionController extends BaseRestController {
private static final Log logger = LogFactory.getLog(IngestionController.class);
// This variable is getting "null" autowiring if invoked
// via Spring Unit Testing framework while it is injected fine via
// Spring Boot app invocation.
@Autowired
public IngestionPayloadProcessor payloadProcessor;
@RequestMapping(path = "/ingest", method = RequestMethod.PUT,
consumes = {
MediaType.APPLICATION_JSON_VALUE,
MediaType.APPLICATION_XML_VALUE
},
produces = {
MediaType.APPLICATION_JSON_VALUE
})
public IngestionSuccessResponse ingest(@RequestHeader(value = "authToken", required = true) String authToken,
@RequestBody String jsonBody) throws Exception {
IngestionPayload ingestionPayload = new IngestionPayload();
ingestionPayload.setAuthToken(authToken);
ingestionPayload.setJsonBody(jsonBody);
IngestionSuccessResponse ingestionSuccessResponse = payloadProcessor.process(ingestionPayload);
return ingestionSuccessResponse;
}
}
服务等级
package com.initech.myapp.ingestion.app.service;
@Service
@ImportResource({"spring.xml"})
public class IngestionPayloadProcessor {
private static final Log logger = LogFactory.getLog(IngestionPayloadProcessor.class);
@Resource(name = "kafkaConfig")
private Properties kafkaConfig;
@Value("${kakfaTopic}")
private String kakfaTopic;
public IngestionSuccessResponse process(IngestionPayload ingestionPayload) throws Exception {
try {
IngestionSuccessResponse ingestionSuccessResponse = buildSuccessResponse(ingestionPayload);
return ingestionSuccessResponse;
}
catch (IllegalStateException e)
{
logger.error("Encountered exception while dropping message in Kafka... " + e.getMessage());
throw e;
}
}
}
private buildSuccessResponse() { ... }
Spring单元测试
@RunWith(SpringRunner.class)
@ContextConfiguration(locations = "classpath*:/spring.xml")
@WebMvcTest(IngestionServerApplication.class)
public class IngestionServerApplicationTests {
@Autowired
private MockMvc mockMvc;
@Before
public void setUp() throws Exception {
mockMvc = MockMvcBuilders.standaloneSetup(
new IngestionServiceController())
.build();
}
@Test
public void testIngestService() throws Exception {
HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders.add("authToken","safdafio12312asdfs23");
RequestBuilder requestBuilder = put("/ingest").content("{'testKey' : 'testVal'}").accept(MediaType.APPLICATION_JSON).headers(httpHeaders);
this.mockMvc.perform(requestBuilder).andExpect(status().isOk());
}
}
错误日志
2016-08-10 19:24:36.500 DEBUG 7505 --- [ main] m.m.a.RequestResponseBodyMethodProcessor : Read [class java.lang.String] as "application/json" with [org.springframework.http.converter.StringHttpMessageConverter@49aa766b]
2016-08-10 19:24:36.510 DEBUG 7505 --- [ main] .m.m.a.ExceptionHandlerExceptionResolver : Resolving exception from handler [public com.initech.myapp.ingestion.model.IngestionSuccessResponse com.initech.myapp.ingestion.app.controller.myappIngestionServiceController.ingest(java.lang.String,java.lang.String,java.lang.String,java.lang.String,java.lang.String,java.lang.String,java.lang.String,java.lang.String) throws java.lang.Exception]: java.lang.NullPointerException
2016-08-10 19:24:36.512 DEBUG 7505 --- [ main] .m.m.a.ExceptionHandlerExceptionResolver : Invoking @ExceptionHandler method: public org.springframework.http.ResponseEntity<java.util.Map<java.lang.String, java.lang.Object>> com.initech.myapp.base.controller.BaseRestController.handleException(java.lang.Exception,javax.servlet.http.HttpServletRequest)
This is the error handler...
2016-08-10 19:24:36.514 INFO 7505 --- [ main] p.d.i.a.c.myappIngestionServiceController : > handleNoResultException
2016-08-10 19:24:36.574 DEBUG 7505 --- [ main] o.s.w.s.m.m.a.HttpEntityMethodProcessor : Written [{status=500, authToken=6acb1a5c-2ced-4690-95b3-eb7957c7c28a, error=null}] as "application/json" using [org.springframework.http.converter.json.MappingJackson2HttpMessageConverter@50d3bf39]
java.lang.AssertionError: Status
Expected :200
Actual :500
注意,我已经通过测试进行了调试,我可以看到,当payloadProcessor对象为null时,在Rest控制器类的下面一行抛出了NullPointer异常。
IngestionSuccessResponse ingestionSuccessResponse = payloadProcessor.process(ingestionPayload);
=====
@RunWith(SpringRunner.class)
@ActiveProfiles("dev")
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@AutoConfigureMockMvc
public class IngestionServerUnitTests {
@Autowired
private MockMvc mockMvc;
@MockBean
private IngestionPayloadProcessor processor;
// I was able to get this to work by removing the setUp() method
// that was originally in my code. It was trying to build a new instance
// of the REST controller and then run the "perform" on top of it
// which was causing the test to fail I assume!
/*@Before
public void setUp() throws Exception {
mockMvc = MockMvcBuilders.standaloneSetup(
new IngestionServiceController())
.build();
}*/
@Test
public void testIngestService() throws Exception {
IngestionSuccessResponse ingestionSuccessResponse = new IngestionSuccessResponse();
ingestionSuccessResponse.setStatusCode("200");
ingestionSuccessResponse.setRequestId("6acb1a5c-2ced-4690-95b3-eb7957c7c28a");
ingestionSuccessResponse.setReceivedTimestamp("2016-08-09T19:43:30.02234312");
given(this.processor.process(anyObject())).willReturn(ingestionSuccessResponse);
HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders.add("Authorization","5e18685c95b34690");
RequestBuilder requestBuilder = put("/ingest").content("<test>test data</test>").accept(MediaType.APPLICATION_JSON).headers(httpHeaders);
this.mockMvc.perform(requestBuilder).andExpect(status().isOk());
}
}
当您指定@WebMvcTest
时,只有应用程序的某些组件被添加到Application ationContext中。注释实际上是文档中描述的一堆其他注释的组合:https://docs.spring.io/spring-boot/docs/current/api/org/springframework/boot/test/autoconfigure/web/servlet/WebMvcTest.html
基于此,IngestionPayloadProcessor
不会被实例化为bean,也不应该被实例化,因为您告诉测试只运行web层的测试。您需要做的是在测试中为IngestionPayloadProcessor
指定一个@MockBean
,然后为控制器调用的方法定义一个mock。
@RunWith(SpringRunner.class)
@WebMvcTest(IngestionServerApplication.class)
public class IngestionServerApplicationTests {
@Autowired
private MockMvc mockMvc;
@MockBean
private IngestionPayloadProcessor processor;
@Test
public void testIngestService() throws Exception {
given(this.processor.process(anyObject())).willReturn(new InjestionSuccessResponse());
HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders.add("authToken","safdafio12312asdfs23");
RequestBuilder requestBuilder = put("/ingest").content("{'testKey' : 'testVal'}").accept(MediaType.APPLICATION_JSON).headers(httpHeaders);
this.mockMvc.perform(requestBuilder).andExpect(status().isOk());
}
}
有关Spring Boot 1.4测试新功能的详细信息如下:https://spring.io/blog/2016/04/15/testing-improvements-in-spring-boot-1-4
*根据评论更新*
意识到你可以自动配置MockMvc,而不需要使用TestRestTemboard。我还没有测试过这个,但它应该可以工作。
@RunWith(SpringRunner.class)
@SpringBootTest
@AutoConfigureMockMvc
public class IngestionServerApplicationTests {
@Autowired
private MockMvc mockMvc;
@Test
public void testIngestService() throws Exception {
HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders.add("authToken","safdafio12312asdfs23");
RequestBuilder requestBuilder = put("/ingest").content("{'testKey' : 'testVal'}").accept(MediaType.APPLICATION_JSON).headers(httpHeaders);
this.mockMvc.perform(requestBuilder).andExpect(status().isOk());
}
}
我想测试一些包含其他autowired服务的服务。但这些“外部”服务对于测试本身并不是必需的。 我如何创建一个测试设置,例如下面的例子? ApplicationContext.xml: 问题:所有包含来自未在测试中扫描的包(如)的自动连线依赖项的服务都将引发异常: 原因:org.springframework.beans.factory.NoSuchBeanDefinitionException:
我正在尝试创建一个Spring Boot测试类,它应该创建Spring上下文,并自动连接服务类以供我测试。 这就是我得到的错误: 原因:org。springframework。豆。工厂NoSuchBeanDefinitionException:没有“com”类型的合格bean。目瞪口呆。戈布斯。基础服务FileImportService'可用:至少需要1个符合autowire候选资格的bean。依
我创建了使用jdbc处理DB的Dao存储库。 我在我的服务类中自动连接了这个存储库。 然后我尝试在测试类中自动连接我的服务类。 当我开始测试时,我得到下一个错误: 服务中构造函数的参数0需要找不到DaoImpl类型的bean。 我如何解决我的问题?
问题内容: 如果Service类使用Validated注释进行注释,则同一类无法自动装配自身。 这是在Spring Context尝试加载时引发的异常: 同样,当您有很多依赖于类的自身时,就会发生这种情况(当某个服务使用使用第一个服务的其他服务时)。我想知道@Validated注解,但是我总是在bean上遇到同样的错误。 有人知道我该怎么解决吗? 问题答案: 在这种情况下,注释与错误的自动装配无关
我在服务类中的自动连线 Bean 上收到 。我尝试自动连线的类是Cassandra Repository。 我的主要类应用程序.java 我的Cassandra配置CassandraConfig.java 我的仓库(dao)产品价格仓库.java 我的服务类ProductService.java 我的pom.xml 我的理解是,注释应该拿起存储库并根据注释创建 bean。ProductServic
我有一个应用程序,公开Websocket/SockJS/Stomp服务器endpoint,并想运行一个JUnit测试,运行客户端(JavaSTOMP客户端,也来自Spring)对它,以测试发送功能。 我有一个测试,如 在这里,我从 https://github.com/jhalterman/concurrentunit 中使用了Water,其效果基本上是将测试的主线程延迟到辅助线程调用reachi