Step 3: Adding some tests
The app doesn't have any tests yet. But before we write our first test, let's create an abstract test class for the things which are common between the tests.
Abstract class
Add com.example.demo.support.AbstractIntegrationTest
class to src/test/java
sourceset. It should be an abstract class with standard Spring Boot's testing framework annotations on it:
Our very first test
Now we need to test that the context starts.
Add com.example.demo.DemoApplicationTest
, extend it from your base class you just created and add a dummy test:
Run it and verify that the application starts and the test passes. Spring will detect H2 on the classpath and use it as an embedded DB.
This is already a useful smoke test since it ensures, that Spring Boot is able to initialize the application context successfully.
Populate the database
The context starts. However, we need to populate the database with some data before we can write the tests.
Let's add a src/test/resources/schema.sql
file with the following content:
Now run the test again. Oh no, it fails!
It seems that H2 does not support the PostgreSQL SQL syntax, at least not by default.
Last updated