I am new in springboot. I am just watching the Spring in Action and programming follow the author. then things get difficult when i just reading the chapter 1. I need to test a controller. the code in this book is: I am using springboot 2.7.3 so i just remove @RunWith(SpringRunner.class) from my code. and i immediately get an error
Tag: spring-test
Is it possible to retrieve value from application.properties without autowiring the class under test?
I have a test class that is annotated with @Spy and @InjectMocks and tested using Mockito. The class under test has a value (url) that is retrieved from the application.properties file. I’d like to test whether this url is being set correctly within the method that uses it. I can do this if I remove the @Spy and @InjectMocks annotations
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
Set value for private final static inlined variable using Mockito
I’m trying to test a method which calls another method which makes use of a private final static variable initialized inline. I’m then trying to change the value for the variable when the tests run. I’ve been trying things such as spring reflection or adding a setter for that specific field but maybe I should use something as powermock, which
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
Getting NPE on ControllerTest using SpringBoot WebFluxTest and JUnit4
I’ve this project structure: And I am trying to test a dummy method on the Controller. The funcionality is very simple. You send a String by POST and is returned with a + “123” CustomerServiceImpl.java CustomerController.java And the controller test class: CustomerControllerTest.java Then, when I test the exepectSatus().isOk() the test is passed: But if I add the rest of funcionality
Create @MockBean with qualifier by annotating class?
In my Spring Boot test I’m using 2 mock beans with different qualifiers: Since I’m not using these beans explicitly, I would rather move them away from the class body, as the @MockBean annotation is now repeatable: However, I need to pass in a qualifier as well, since they have the same type. Any idea on how I can achieve
Unknown data type when using an integer over NamedParameterJDBCTemplate on H2 [SPRING-BOOT]
I’m testing a Dao with an In-Memory DB with H2. I’m passing an int to the query with a map to execute it. This query is working OK on Oracle SQL, but is not succeding in H2. DAO QUERIES When I try to execute this query on H2, it returns: Error I tried to execute the query hardcoding the int