代码之家  ›  专栏  ›  技术社区  ›  suman j

spock中的springbootstest restcontroller测试结果为404

  •  0
  • suman j  · 技术社区  · 7 年前

    下面给出了控制器的代码,当我启动Spring引导应用程序时,我可以对资源/foo/id/id进行HTTP调用以获取数据。

    然而,来自集成测试的相同调用返回404。调用未触发REST控制器方法。测试未将HTTP调用转发到REST控制器。我错过了什么?

    @RestController
    @RequestMapping(“/foo”)
    class FooResource {
    @RequestMapping(method = RequestMethod.GET, path = “/id/{id}”)
    String getData(@PathVariable int id) {
        logger.error("===== FooResource.getData called with {}", id)
         // more code
    }
    //more code
    
    //Spock test case
    @ContextConfiguration
    @SpringBootTest(
            webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT
    )
    @EnableWebMvc
    @AutoConfigureWebClient
    @TestPropertySource(locations = "classpath:application-test.properties")
    class IntegrationTest extends Specification {
    
        @Autowired
        RestTemplate restTemplate 
    
    
       @Configuration
       @ImportResource(["classpath*:/test-properties.xml", "classpath*:/springintegration-config.xml"])
       static class Beans {
            @Bean
            MessagingTemplate messagingTemplate() { new MessagingTemplate() }
    
            @Bean
            ServletWebServerFactory servletWebServerFactory() {
                return new TomcatServletWebServerFactory(9010);
            }
    
            @Bean
            public RestTemplate restTemplate(RestTemplateBuilder restTemplateBuilder) {
                return restTemplateBuilder
                  .setConnectTimeout(30 * 1000)
                    .setReadTimeout(30 * 1000)
                   .build();
            }
       }
    
       def ‘foo resource returns the expected data for Id'() {
        given:
        int id = new SecureRandom().nextInt()
        TestRestTemplate restTemplate = new TestRestTemplate();
    
        when:
        ResponseEntity<String> response = restTemplate.getForEntity("http://localhost:9010/foo/id/1234", String.class)
    
        then:
        assert response.statusCode == HttpStatus.OK
    }
    

    测试用例运行日志包括以下已可用的映射

    s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped “{[/foo/id/{Id}],methods=[GET]}" onto public java.lang.String com.foo.bar.rest.FooResource.getData(int)
    2018-06-15 13:54:38.680 DEBUG 20710 --- [           main] o.s.b.f.s.DefaultListableBeanFactory     : Finished creating instance of bean 'requestMappingHandlerMapping'
    2018-06-15 13:54:38.680 DEBUG 20710 --- [           main] o.s.b.f.s.DefaultListableBeanFactory     : Creating shared instance of singleton bean 'mvcPathMatcher'
    2018-06-15 13:54:38.681 DEBUG 20710 --- [           main] o.s.b.f.s.DefaultListableBeanFactory     : Creating instance of bean 'mvcPathMatcher'
    
    1 回复  |  直到 6 年前
        1
  •  0
  •   suman j    6 年前

    //Spock test case
    @SpringBootTest(
            classes = TestConfig.class,
            webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT
    )
    @AutoConfigureWebClient
    @TestPropertySource(locations = "classpath:application-test.properties")
    class IntegrationTest extends Specification {
    
        @Autowired
        RestTemplate restTemplate 
    
    
       @Configuration
       @ImportResource(["classpath*:/test-properties.xml", "classpath*:/springintegration-config.xml"])
       static class Beans {
            @Bean
            MessagingTemplate messagingTemplate() { new MessagingTemplate() }
    
            @Bean
            ServletWebServerFactory servletWebServerFactory() {
                return new TomcatServletWebServerFactory(9010);
            }
    
            @Bean
            public RestTemplate restTemplate(RestTemplateBuilder restTemplateBuilder) {
                return restTemplateBuilder
                  .setConnectTimeout(30 * 1000)
                    .setReadTimeout(30 * 1000)
                   .build();
            }
       }
    
       def ‘foo resource returns the expected data for Id'() {
        given:
        int id = new SecureRandom().nextInt()
        TestRestTemplate restTemplate = new TestRestTemplate();
    
        when:
        ResponseEntity<String> response = restTemplate.getForEntity("http://localhost:9010/foo/id/1234", String.class)
    
        then:
        assert response.statusCode == HttpStatus.OK
    }