Skip to content

Tag: spring-test

How do I write a test for a service that uses other services in Springboot?

I have a BookManagementService that uses the @Autowired implementation of three other services, like so So how do I mock the aforementioned services and their repo in the BookManagementServiceTest? When the test start running and it gets to the yearService layer, it throws a NullPointerEXception cause the year it receives is null The BookManagementServiceTest Answer You just have to mock

How to combine Testcontainers with @DataJpaTest avoiding code duplication?

I want to use Testcontainers with @DataJpaTest (and @SpringBootTest) using JUnit 5. I have the basic setup working using the @Testcontainers and @Container annotation like this: See for the full example code (AtleteRepositoryTest, TeamRepositoryTest and TestcontainersDatajpatestApplicationTests). To avoid the repetition of declaring the PostgreSQL container and the dynamic properties, I tried the following: JUnit 5 extension Baeldung has a

Overriding beans in Integration tests

For my Spring-Boot app I provide a RestTemplate though a @Configuration file so I can add sensible defaults(ex Timeouts). For my integration tests I would like to mock the RestTemplate as I dont want to connect to external services – I know what responses to expect. I tried providing a different implementation in the integration-test package in the hope that