Reputation: 1753
I am writing API test cases for one of my controllers, but it is resulting with a 404. I thought it would be a typo but it is not. Below are the code snippets.
RestController: 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");
}
}
My Test 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());
}
}
My app uses spring security and to bypass that I have created a TestAPP
class so that it will help me build only the config without security.
TestApp: package com.x.y.address (src/test)
@EnableAutoConfiguration(exclude = { SecurityAutoConfiguration.class})
// @ComponentScan({"com.x.y.address.controller.AddressInternalController"})
public class TestApp {
}
Above are the structure of the class.
Initially I thought may be the program does not scan the controller package and hence the 404. Hence added the componentScan. But that did not help.
Searched through a lot of stack over flow but most of the 404 are due to a type but it is not in my case.
Error log:
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
Any help shall be greatly appreciated.
Upvotes: 1
Views: 161
Reputation: 2706
I replaced:
@EnableAutoConfiguration(exclude = { SecurityAutoConfiguration.class})
// @ComponentScan({"com.x.y.address.controller.AddressInternalController"})
public class TestApp {
}
with:
@SpringBootApplication(exclude = { SecurityAutoConfiguration.class})
// @ComponentScan({"com.x.y.address.controller.AddressInternalController"})
public class TestApp {
}
and it worked.
UPDATE 1:
I noticed, in your @ComponentScan
you use the path to the class itself, but you should point to the package with your controller. If you want to specify a class, use basePackageClasses
property of @ComponentScan
Upvotes: 1