Integrating Tests into app

Hi everyone,

I’m currently diving into testing and would like to apply it to my Vaadin/Spring Boot app. My goal is to learn more about it and gain a deeper understanding for future use. A positive side effect is that it improves code quality—but are there other advantages? To me, it feels like testing adds more work.

I’m unsure where to start. Should I test every method?

Later on, I’d like to test the frontend as well to avoid manually navigating through the app and testing buttons, etc.

It seems a bit complicated to create a mocked database using an .sql script that loads on startup. Is there a simpler way to mock the data returned by a service? @SimonMartinelli once shared a link about database mocking, but I can’t find it anymore. Most of my methods depend on services fetching data from a database.

I’d really appreciate any input on where and how to start. The tutorials and technologies out there feel overwhelming and confusing.

I always use Testcontainers for testing. And I setup the database with Flyway.

Here is an example: GitHub - martinellich/registration

1 Like

Thank you, Simon. What if i dont have spring.jpa.defer-datasource-initialization=true because i dont use hibernate (Using JDBCClient, because most time the sql queries are created by a team member)?

I presume you have an existing application without tests, and nobody has given you any requirements about test coverage. In that case, start by figuring out what it is the most critical functionality of your application and write a test for that. Once that’s working, pick the second most critical functionality, and so on. At one point, you’ll notice you’ve reached a point where you’ve covered x% of the most important functionality of the app, and you have a feeling that adding more tests isn’t necessarily worth the extra effort.

Of course, your application probably isn’t finished yet - otherwise, why bother testing it? Whether you are continuing development of an existing app or starting a new one from scratch, you should consider adopting a test-driven approach; for every new important functionality, write a test first and then implement the functionality so that the test succeeds. This will add a little overhead to your work, but once you get used to it, you won’t even notice the difference. Just make sure that you test the negative cases as well - for example, when you’re testing the log in functionality, remember to not only test that you can log in successfully with the correct credentials, but also that logging in with wrong credentials fails. This is especially important in security-related functions.

I don’t think it’s particularly useful to try to cover every line of code with your tests, at least without any extra information about the application. There are cases where it’s a must, for example in apps where someone’s life may be in danger. In typical cases, it’s more relevant to test the most important things first and there is diminishing value in adding new tests past a certain point. So think about real life use cases more than code lines.

In my opinion, the biggest benefits of having good test coverage are that 1) it gives you confidence to make changes quickly (for example, if there’s a problem in production, you can push a new version if you have automated tests to confirm that everything important still works), and 2) the tests document the codebase. The second part is especially important when you have a long-lived application; you might not remember why you (or someone else) wrote a specific piece of code, but if you change it and a test fails, the test will tell you why someone thought it should work in a certain way.

4 Likes

Awesome thank you Olli! The benefits got me.
There comes also into place like in my previous message, need a mocked database and mocked (external) APIs for example.

@SimonMartinelli wouldnt it be possible using a seperate spring profile like here point 5 Configuring Separate Spring DataSource for Tests | Baeldung

And adding a database setup.sql to it which run on test? That sounds like a possible way

Then don’t use it? Or what exactly is your question?

You don’t need a profile. I never use profiles.

I think this topic is too complicate to discuss in the forum. That would be a perfect fit for a meetup where I could show how I do testing.

1 Like

Btw. did you see that? Guide on Loading Initial Data with Spring Boot | Baeldung

Many methods handles database CRUD, thats why i need a database. But yeah ok i could skip those and dont write tests for those methods. Lets guess i have a method which creates an order and sum the orderpositions for an invoice:

dummycode:

double sumInvoice(Order order ){
//sum
}

The order comes from the database, but i could create an order object in the test with dummy data to mock it. I dont need fetch it from the database to create a test for the calculation method.
Got it.

Haveing a meetup for testing would be really cool. How does your time table look like usually, maybe we find some other interested guys and make a meetup out of the interval.