We have some controller, say something like this:
@Controller
@RequestMapping("/api")
public Controller UserController {
@RequestMapping("/users/{userId}")
public User getUser(@PathVariable String userId){
//bla
}
}
We have an Integration test for this, say:
@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
@SpringApplicationConfiguration(classes= MyApp.class)
@IntegrationTest("server:port:0")
public class UserControllerIT {
@Autowired
private WebApplicationContext context;
@Test
public void getUser(){
test().when()
.get("/api/users/{userId}", "123")
.then()
.statusCode(200);
}
}
How can we avoid hard coding the "/api/users/{userId}" in the test? How can we look up the requestmapping by name. The above request mapping should have a default name of UC#getUser
The only thing I've seen is something like MvcUriComponentsBuilder, which appears to require that it be used within the context of a request (so it would be used in .jsps to generate URLs to controllers).
What is the best way to handle this? Do I have to expose the mappings as static strings on the controllers? I'd prefer to at least avoid that.