Reputation: 893
Assertion error using @RequestMapping annotation outside of the class
I am getting this error message:
java.lang.AssertionError: Status
Expected :200
Actual :404
My Controller is like this
@Service
@RestController
@RequestMapping("/execute/files")
@ResponseBody
public class ControllerFiles {
@Autowired
@Qualifier("fileRunner")
ProcessRunnerInterface processRunnerInterfaceFiles;
public InputState executeRestFile(@RequestParam String name) throws ExecutionFailedException, URISyntaxException {
///code///
}
public List<String>....{
///code///
}
}
My Test
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest
@AutoConfigureMockMvc
public class ControllerFilesTest {
@Autowired
private MockMvc mockMvc;
@Autowired
ControllerFiles controllerFiles;
@Test
public void testSpringMvcGetFiles() throws Exception {
this.mockMvc.perform(get("/execute/files").param("name", "Spring Community Files"))
.andDo(print()).andExpect(status().isOk());
}
}
But when I have my code like this the test work fine!
@Service
@RestController
public class ControllerFiles {
@Autowired
@Qualifier("fileRunner")
ProcessRunnerInterface processRunnerInterfaceFiles;
@RequestMapping("/execute/files")
@ResponseBody
public InputState executeRestFile(@RequestParam String name) throws ExecutionFailedException, URISyntaxException {
///code///
}
public List<String>....{
///code///
}
}
Any ideas what is going wrong?
Upvotes: 5
Views: 10957
Reputation: 9622
The methods in your RestController need to be marked as @RequestMapping if you want them to be picked up as request resources. If you want to keep the base request mapping at the controller level as in your first RestController then you need to do the following:
@RestController
@RequestMapping("my/path")
public class MyController {
@RequestMapping("/")
public InputState myMethod() {
...
}
}
Upvotes: 1
Reputation: 2152
As it is said in documentation:
In the above example, @RequestMapping is used in a number of places. The first usage is on the type (class) level, which indicates that all handler methods in this controller are relative to the /appointments path.
So the class level @RequestMapping
is only indicating relativnes. It is not declare actual resource paths based on public methods only. So you need to annotate your method like this:
@GetMapping
public InputState executeRestFile(@RequestParam String name) throws Exception {
// omited
}
Or like this:
@RequestMapping(method = RequestMethod.GET)
public InputState executeRestFile(@RequestParam String name) throws Exception {
// omited
}
Upvotes: 0