blog podcast

Integration Testing

I’m all for it. Well, not too much, but sufficiently. But, as I’ve experienced today, sometimes when working with Spring Boot there can be problems in the way.

The thing is, in my project, JPA is being used. And generated repositories are used where queries are defined by the name of functions. I think this is a horrible library, a really bad idea. Why? Because it makes it hard to isolate the logic into the repository. I mean simple logic can be created with these interfaces, but what if you want to use a covering index and not select everything? Or what if you want to do paging? For the covering index you could of course create another interface with another entity class, but in the end you are kind of forced to structure your code according to your DB structure, you end up with an anemic model. In my case it was because of the paging. You can do this, but you have to use a PageRequest, which means that some of the logic from your query bleeds out into your services. Bad information hiding. So this bleading was the reason that I wanted to do an integration test to just not have to bother with mocking the interface of my repository that I felt was becoming more complex than I wanted it to be. And here is where I again meet the limitation of (my knowledge of) spring boot. It feels like everything is designed for specific use cases, and as if the code is trying actively to prevent you from implementing any special use case for yourself. I wanted to use DataJpaTest, that’s a great annotation you can use that rollbacks your db changes between each test. Great for integration testing of repositories. BUT. It doesn’t add components of other types than @Repository to the application context. This means that my service that was having the repository logic bled into it couldn’t be autowired. OK, then you can use SpringBootTest, this wires everything. BUT. It doesn’t rollback your DB changes or give you access to the EntityManager. Grrr. In the end the unit tests became the way. Seems I once again wanted to do something that spring boot doesn’t want me to.

Code of the Day

@DataJpaTest

See, fixes everything. Almost :(.