Smart package structure to improve testability

There are many ways of dividing whole application into packages. Discussions about pros and cons of packaging by feature or by layer can we found on many programming blogs and forums. I want to discuss this topic starting from testability and see if it will drive to any meaningful result.

At the beginning let's try to describe what we usually want to test in our applications across different layers. Let's assume standard three tier architecture. At the bottom we have data layer.

Depending on our attitude to Domain-Driven-Design we'll try to maximize (for rich, business-oriented entities) or minimize (for anemic entities built only from getters and setters) test coverage. In the second approach it's even hard to say about any tests, unless you don't trust Java and want to verify if get can retrieve value assigned before by set invocation. For rich entities we definitely want to verify business logic correctness. But to be honest it almost always can be done by simple unit tests with proper mock setup. There are often thousands of tests in this layer, so we want them to be maximally fast. That's a great field for unit testing frameworks! Wait? Why don't you want to test entities with database? I can ask opposite question - why should I do? To verify if JPA or any other persistence API are still working? Of course there are always some really complex queries that should be verified with real database underneath. For those cases I'll use integration tests on repository level. Just database + repository + entities. But remember about single responsibility. Your integrational tests is checking only query - leave whole entity logic for unit tests.

Next layer is usually built from services. In DDD services are just working with repositories to load entities and delegate them whole business logic processing. As you can predict those tests will be pretty simple. Do you think we need database here? Will it provide any added value? Don't think so. And what about second scenario? Anemic entities in our model? Whole logic is concentrated in services so we have to accumulate our test coverage in this layer. But as we already discussed with domain logic it can we done without using external resources. One more time - all we need is a unit test. So still no database. We can run all tests based on repositories mocks. No problems with managing datasets leading to "expected 3 but found 2" tests failures. Just because some other test committed one more order with value between 200$ and 300$. Even if we want to use IoC framework here, it can simulate repository layer with mocks. Without proper decoupling from data layer framework would automatically load repositories via some scanning mechanism. And it's not something we want.

On top of the services we usually place something allowing users to use our application. It can we fronted, RESTful API, SOAP services, etc. What is important to check here? To be fair with our customers we should stick to the contract we have with them. This whole can be material for separate blog post, but narrowing down to REST services:
 "If you will send we POST request to /users URL I'll answer with list of all users. Every user will have id as an integer and string with username."
OK - that looks as a contract. So what should we check in this layer? Of course if this contract is valid. Send HTTP request and verify if response contains array of users, from which every entry is build from integer ID and string username. Can we do it on top of the services mocks? Sure :)

So to encapsulate everything:

  • data layer = unit tests for logic and integration tests with DB for complex query validation
  • service layer = unit tests for logic and light integration tests without DB for testing IoC framework dependent logic
  • front layer = integration tests without DB to verify customer contract

So far we've described in details what is worth to test on different levels. Now let's move to feature based packaging. It definitely helps to keep the code well organized when it's build around different business contexts. For large applications it's something that allows you to cut it down into many modules or even many applications. Without such feature layout such actions will require huge refactorings before. But is it still needed after splitting our monolith into applications? Just think about starting new application. What will be its base package? It's nothing else than a feature packaging :) But would you stop on this base package, or still will you split in into layers? As you see those two structures can live together.

For layer based structure our application will look like below:

For feature based we'll get something like

But usually as business logic always grows, it leads to splitting whole application into modules or services, so finally we get:

To sum up. In my opinion packaging by layer is a must. It allows us to test each layer separately and keep our tests well organized. Package by feature is really useful in bigger projects. For microservices which are built around single bounded context more detailed division can lead to uncomfortable navigation. However code inside feature package should be still broken on layers for the same reason as mentioned above. Especially with Spring Framework layer-based structure helps us with setting useful component-scan, and won't drive us to setup a database just because we want to start context with two services. In my GitHub repository you can find sample project based on Spring.


Eduards Sizovs said…
Jakub, thanks for article. Quick fix: Bundled context -> Bounded context.

codingdolphin said…
Hi there to everyone, the contents present at this web page are actually amazing for people knowledge, well, you can also visit Best Programming Blogs for more Coding Dolphin related information and knowledge. Keep up the good work.
JLS Automation said…
I'd like to express my gratitude for writing such an informative piece on this blog. This article provided me with a variety of data about Pick and Place Packaging. Continue to post.
Very well written article. It was an awesome article to read. Complete rich content and fully informative. I totally Loved it. Read more info about VFFS Baggers
John Clark said…
This information is so useful and informative which you have shared here. It is beneficial for beginners to develop their knowledge. It is very gainful information. Thanks for sharing! here's something you can check map packaging. Thank you.
I'm glad I came to this page because it contains a lot of useful information that you have offered here. Packaging Market Consultancy I appreciate you sharing this content.
Nancy said…
Consumer choices are made in stores 70% of the time. (This offers an endless chance to convey the tale of your product, so long as you can keep their attention.) Polyester film supplier

Popular posts from this blog

Using jstat to report custom JVM metric sets

Understanding Spring Web Initialization

Including Java agent in standalone Spring Boot application