Joe
Joe

Reputation: 4625

Should I unit test generated Java code?

Simple question. If I use spring-data to generate CRUD methods for my DAO layer, should I still write unit tests against the generated methods? Or would that be the equivalent of unit testing library code?

Thanks in advance.

EDIT: To clarify, I'm asking whether or not the unit test needs to be written in addition to a suite of integration tests that get run before a release. For example, a unit test for the findAll() method of the DAO layer would be similar to the following:

class DepartmentDAOTest extends spock.lang.Specification {
   /* ... */
   def "returns all departments"() {
       setup:
       def result = new List<Department>()
       when:
       result = dao.findAll()
       then:
       result.size() == EXPECTED_SIZE
   }
}

Whereas an integration test would be run probably by a test team or developer by hand, possibly before tagging a new release. This could either be automated using JWebUnit or Geb, and tests every component (including the platform) to ensure they work as expected when "integrated."

If I were to write the DAO implementation by hand using JdbcTemplate there would be no question that I should unit test every method. When I unit test the service layer (which makes calls to the DAO layer) I can mock out the DAO layer so I don't test it twice.

If I make a call into a third-party library like pdfbox for generating a PDF, there's an expectation for each method to work (because it is tested as a part of the pdfbox project). I don't test that their drawSquare method really draws a square, but during integration testing I'll see that my export PDF functionality correctly exports a PDF the way we want it to.

So the question should really be re-worded as, "Under which testing phase should I test my usage of spring-data?"

Upvotes: 2

Views: 774

Answers (2)

Oliver Drotbohm
Oliver Drotbohm

Reputation: 83171

First, there is no code generated at all. We built a query meta model from the query methods you declare and dynamically execute these queries. The short answer here is: you definitely should test these methods declared. The reason is as obvious as it is simple: the query method declarations - no matter if they use derived queries or manually declared ones - interact with the mapping metadata you defined for your entities. Thus, it's definitely reasonable to check the query method execution to make sure you see the expected results. This then of course an more of an integration test and a semantical check for the queries executed, rather than a classical unit test.

Upvotes: 4

user207421
user207421

Reputation: 311052

No. As a general rule, don't test the platform.

Upvotes: 4

Related Questions