Skip to content
Advertisement

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

POST: Exception Status expected: but was:

Technically it should be a simple task, but I can’t find the error. I want to write a normal “POST method”, but when I tested it, it came to a problem: enter code here Status expected:<201> but what:<200>. My question is, why do I get an OK and not a CREATED? CODE: PostMapping in Controller Unit Test Answer Because for

testEntityManager.persistFlushFind() giving error “detached entity passed to persist”

I was trying to test h2 Repository with spring-boot-testing .Below is the Testing code is used.: In the above code I am facing this “detached entity passed to persist”. “testEntityManager.persistFlushFind(new Product(1,”A”,”dec”,false))” is giving below error: Please tell how can i resolve the issue.For more info please comment Answer You didn’t show your Product entity. But I assume that the ID

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 https://github.com/wimdeblauwe/blog-example-code/tree/feature/testcontainers-datajpatest/testcontainers-datajpatest 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

Advertisement