1

我在 1.1.2.RELEASE 版本中使用Spring Rest Docs

考试

MvcResult result = this.mockMvc.perform(get("/api").accept(MediaType.APPLICATION_JSON))
                .andExpect(status().isOk())
                .andDo(print())
                .andDo(document("index"))
                .andReturn();

API

@RequestMapping(value = "/api")
    public ResponseEntity<String> apiWelcome() {
        final HttpHeaders httpHeaders= new HttpHeaders();
        httpHeaders.setContentType(MediaType.APPLICATION_JSON);
        return new ResponseEntity<String>("{\"api\": \"test1\"}", httpHeaders, HttpStatus.OK);
    }

当我运行应用程序时,我可以在浏览器中访问“/api”并获得预期的{"api": "test1"}响应。

但是,如果我运行测试,我会得到以下日志条目:

c.i.crawler.testnet.measure.RestDocsTest INFO  Started RestDocsTest in 2.96 seconds
o.s.web.servlet.PageNotFound WARN  No mapping found for HTTP request with URI [/api] in DispatcherServlet with name '' [main] 

并且测试失败是因为HTTP/1.1 404 Not Found. 我究竟做错了什么?

申请开始

@SpringBootApplication
@Configuration
@EnableConfigurationProperties
@EnableAutoConfiguration
@ComponentScan(basePackageClasses = { SomeController.class })
public class AppRUN {

    public static void main(String[] args) {
        SpringApplication.run(AppRUN.class, args);
    }

} 
4

1 回答 1

3

我的测试类注释如下:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes=RestDocsTest.class)
@WebAppConfiguration

将注释更改为以下内容可消除错误:

@RunWith(SpringRunner.class)
@SpringBootTest
于 2016-09-15T15:01:19.470 回答