我正在尝试使用 springrunner 编写集成测试。我将它作为 springboottest 运行并使用 autoconfiguremockmvc。但是,我不断收到 404。似乎我的控制器/端点没有被 autoconfiguremockmvc 加载。有谁知道如何将其连接起来以便接收我的控制器的解决方案?
我确实在我的测试类中添加了一个基本控制器,并且我能够成功地击中它,但到目前为止我一直无法击中我想在集成测试中使用的实际控制器。
@RunWith(SpringRunner.class)
@SpringBootTest(classes = { TestConfig.class })
@TestPropertySource(locations = "classpath:application-test.properties")
@AutoConfigureMockMvc
public class ControllerTest {
@Autowired
private MockMvc mockMvc;
@Test
public void test() throws Exception {
MockHttpServletRequestBuilder request =
MockMvcRequestBuilders.post(URL).contentType(MediaType.APPLICATION_JSON_UTF8_VALUE)
.accept(MediaType.APPLICATION_JSON_UTF8_VALUE)
.content(json);
final ResponseEntity<String> response =
new ResponseEntity<>("works", HttpStatus.OK);
final ResultActions result = this.mockMvc.perform(request).andDo(print());
result.andExpect(status().isOk())
.andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8_VALUE));
}
}
@TestConfiguration
@EnableAutoConfiguration
public class TestConfig {
}