Reputation: 4435
I have a spring boot rest controller which returns an object. Something like this:
@RestController
@RequestMapping("books-rest")
public class SimpleBookRestController {
@GetMapping("/{id}", produces = "application/json")
public ResponseEntity<Book> getBook(@PathVariable int id) {
return findBookById(id);
}
}
I have a unit test that tests this controller method (mockito mocking the service calls inside). Something like:
@RunWith(MockitoJUnitRunner.Silent.class)
public class SimpleBookRestControllerTest {
@Autowired
@InjectMocks
private SimpleBookRestController simpleBookRestController;
@Mock
private MyService myservice;
I want now to test what spring exactly does with the object being deserialized to a JSON string. In my test, I just test the objects, which is fine. But I have now a bug which I want to see the JSON string in a unit test.
How would you do that?
Upvotes: 0
Views: 1193
Reputation: 1012
You'll need more than a simple unit test -- the SimpleBookRestController
does not handle the conversion to JSON, so you can't just unit test it.
See this guide: https://spring.io/guides/gs/testing-web/, or look for examples of using @WebMvcTest
, which is a way to test the whole web layer, including REST calls and their responses.
Upvotes: 1