I’m writing a web service that returns JSON, and I was wondering, if it’s worth unit testing values of a response, or it’s more likely that I’m testing the framework that does the marshalling by doing that?
Right now there are tests written like this :
@Test
public void getPersonWithAnIdNameShouldBeBob() {
expect().body("name", equalTo("bob")).when().get("/getperson/1");
}
Should I focus more on the business logic, the method calls, etc?
It depends, but:
Right now this looks like an integration test, not a unit test, so far.
As such, it’s a valid test: you’re testing some routing with some known data.
Unit testing would test the service in isolation, bypassing the web layer, and look for things like validation or DB connection error handling, not found handling, etc.
Where those tests live may be dependent on the framework, too, and in some cases may not provide the best ROI if they’re handled by the framework.
Test whatever is the most fragile, needs the most documentation, needs to be exercised, etc.