Spring 跑步者测试 return 404 用于 API 测试
Spring runner test return 404 for API test
我正在为我的一个控制器编写 API 测试用例,但结果是 404。
我认为这是一个错字,但事实并非如此。以下是代码片段。
休息控制器:package: com.x.y.address.controller (src/main)
@RestController
public class AddressInternalController {
@PostMapping(value = "/v1/address-service/internal/company/address", produces = "application/json;charset=UTF-8")
@ResponseStatus(OK)
public @ResponseBody ResponseEntity<AddressModel> createCompanyAddress()
throws AddressException, BadRequestException {
return ok("SUCCESS");
}
}
我的测试class:package com.x.y.address.controller (src/test)
@RunWith(SpringRunner.class)
@ContextConfiguration(classes = TestApp.class, initializers = ConfigFileApplicationContextInitializer.class)
@WebMvcTest(controllers = AddressInternalController.class, secure = false)
public class AddressInternalControllerTest {
@Autowired
private MockMvc mvc;
@Before
public void init() {}
@Test
public void createAddressTest_when_invalid_company() throws Exception {
this.mvc.perform(post("/v1/address-service/internal/company/address").contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isOk());
}
}
我的应用程序使用 spring 安全性并绕过我创建的 TestAPP
class 以便它可以帮助我仅构建没有安全性的配置。
TestApp: 包 com.x.y.address (src/test)
@EnableAutoConfiguration(exclude = { SecurityAutoConfiguration.class})
// @ComponentScan({"com.x.y.address.controller.AddressInternalController"})
public class TestApp {
}
以上是class.
的结构
最初我认为可能是程序没有扫描控制器包,因此出现 404。因此添加了 componentScan。但这并没有帮助。
通过大量堆栈溢出搜索,但大多数 404 都是由于类型引起的,但我的情况并非如此。
错误日志:
MockHttpServletRequest:
HTTP Method = POST
Request URI = /v1/address-service/internal/company/address
Parameters = {}
Headers = {Content-Type=[application/json]}
Body = <no character encoding set>
Session Attrs = {}
Handler:
Type = org.springframework.web.servlet.resource.ResourceHttpRequestHandler
Async:
Async started = false
Async result = null
Resolved Exception:
Type = null
ModelAndView:
View name = null
View = null
Model = null
FlashMap:
Attributes = null
MockHttpServletResponse:
Status = 404
Error message = null
Headers = {}
Content type = null
Body =
Forwarded URL = null
Redirected URL = null
Cookies = []
java.lang.AssertionError: Status
Expected :200
Actual :404
非常感谢任何帮助。
我替换了:
@EnableAutoConfiguration(exclude = { SecurityAutoConfiguration.class})
// @ComponentScan({"com.x.y.address.controller.AddressInternalController"})
public class TestApp {
}
与:
@SpringBootApplication(exclude = { SecurityAutoConfiguration.class})
// @ComponentScan({"com.x.y.address.controller.AddressInternalController"})
public class TestApp {
}
成功了。
更新 1:
我注意到,在您的 @ComponentScan
中,您使用 class 本身的路径,但您应该使用控制器指向包。如果要指定 class,请使用 basePackageClasses
属性 of @ComponentScan
我正在为我的一个控制器编写 API 测试用例,但结果是 404。 我认为这是一个错字,但事实并非如此。以下是代码片段。
休息控制器:package: com.x.y.address.controller (src/main)
@RestController
public class AddressInternalController {
@PostMapping(value = "/v1/address-service/internal/company/address", produces = "application/json;charset=UTF-8")
@ResponseStatus(OK)
public @ResponseBody ResponseEntity<AddressModel> createCompanyAddress()
throws AddressException, BadRequestException {
return ok("SUCCESS");
}
}
我的测试class:package com.x.y.address.controller (src/test)
@RunWith(SpringRunner.class)
@ContextConfiguration(classes = TestApp.class, initializers = ConfigFileApplicationContextInitializer.class)
@WebMvcTest(controllers = AddressInternalController.class, secure = false)
public class AddressInternalControllerTest {
@Autowired
private MockMvc mvc;
@Before
public void init() {}
@Test
public void createAddressTest_when_invalid_company() throws Exception {
this.mvc.perform(post("/v1/address-service/internal/company/address").contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isOk());
}
}
我的应用程序使用 spring 安全性并绕过我创建的 TestAPP
class 以便它可以帮助我仅构建没有安全性的配置。
TestApp: 包 com.x.y.address (src/test)
@EnableAutoConfiguration(exclude = { SecurityAutoConfiguration.class})
// @ComponentScan({"com.x.y.address.controller.AddressInternalController"})
public class TestApp {
}
以上是class.
的结构最初我认为可能是程序没有扫描控制器包,因此出现 404。因此添加了 componentScan。但这并没有帮助。
通过大量堆栈溢出搜索,但大多数 404 都是由于类型引起的,但我的情况并非如此。
错误日志:
MockHttpServletRequest:
HTTP Method = POST
Request URI = /v1/address-service/internal/company/address
Parameters = {}
Headers = {Content-Type=[application/json]}
Body = <no character encoding set>
Session Attrs = {}
Handler:
Type = org.springframework.web.servlet.resource.ResourceHttpRequestHandler
Async:
Async started = false
Async result = null
Resolved Exception:
Type = null
ModelAndView:
View name = null
View = null
Model = null
FlashMap:
Attributes = null
MockHttpServletResponse:
Status = 404
Error message = null
Headers = {}
Content type = null
Body =
Forwarded URL = null
Redirected URL = null
Cookies = []
java.lang.AssertionError: Status
Expected :200
Actual :404
非常感谢任何帮助。
我替换了:
@EnableAutoConfiguration(exclude = { SecurityAutoConfiguration.class})
// @ComponentScan({"com.x.y.address.controller.AddressInternalController"})
public class TestApp {
}
与:
@SpringBootApplication(exclude = { SecurityAutoConfiguration.class})
// @ComponentScan({"com.x.y.address.controller.AddressInternalController"})
public class TestApp {
}
成功了。
更新 1:
我注意到,在您的 @ComponentScan
中,您使用 class 本身的路径,但您应该使用控制器指向包。如果要指定 class,请使用 basePackageClasses
属性 of @ComponentScan