Spring测试服务自动装配导致null

我可以运行Rest Controller PUT方法,通过Spring Boot Application按预期方式使用Autowired @Service。 尝试执行Spring JUnit测试时,相同的Autowiring失败。 我曾尝试通过多个线程阅读类似的问题。 我确信我不是通过“new”关键字创建@Service,而是尝试了Context Configuration和其他方法......但都似乎徒劳无功。 我不知道我哪里出错了。

我的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...");    
    }    
}

休息控制器类 -

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

=====

用Mock对象进行单元测试:(这是按预期工作)

@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 ,只有您的应用程序的某些组件添加到ApplicationContext中。 注释实际上是一系列其他注释的组合,如文档中所述:https://docs.spring.io/spring-boot/docs/current/api/org/springframework/boot/test/autoconfiguration/web/的servlet / WebMvcTest.html

基于此,您的IngestionPayloadProcessor不会被实例化为一个bean,而应该不会像您告诉测试只运行Web层的测试一样。 你需要做的是指定@MockBeanIngestionPayloadProcessor测试中,然后定义控制器正在调用该方法的模拟。

@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而不需要使用TestRestTemplate。 我没有测试过,但它应该可以工作。

@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());
    }
}
链接地址: http://www.djcxy.com/p/68081.html

上一篇: Spring Test Service Autowiring resulting in null

下一篇: Spring getting error in get attribute method