I have a Spring controller that looks like this:
@RestController
@RequestMapping("/foo")
public class FooController {
@Autowired
private NamedParameterJdbcTemplate template;
@GetMapping("/{name}")
public List<Foo> byName(@PathVariable String name) {
Map<String, String> params = new HashMap<>();
params.put("name", name);
List<Foo> result = template.query("SELECT * FROM FOOS WHERE FOO_NM = :name", params, new FooRowMapper());
if (result.size() == 0) {
throw new ResponseStatusException(HttpStatus.NOT_FOUND, String.format("foo %s not found", name));
}
return result;
}
}
However, I'm not sure how to test this. I can do a basic "Spring can set it up" test:
@SpringBootTest
public class FooControllerTest {
@Autowired
private FooController controller;
@Test
public void canCreate() {
Assertions.assertNotNull(controller);
}
}
But I'm not certain what the right way to test, for example, the byName method is. Do I need to mock something? Can I just test it like a plain Java method (call it with whatever parameters and assert the output)?