Open Mittal-Shivam opened 2 months ago
What exactly is this supposed to do? If it's meant to change a configuration property in DepartmentService
, which is a singleton bean, I doubt this could ever work, whether with H2 or Postgres database. Please send an example with H2 database so I can take a closer look. Otherwise, the correct solution is to use @TestPropertySource
or @DynamicPropertySource
, not this.
I'm not sure whether the problem should occur when loading data directly in the test class or when calling via rest template, but both methods work for me. However, I had to make an adjustment when asserting the loaded data and remove the zero at the beginning of the expected value. This is likely due to conversion from string to integer type.
Hi @tomix26 ,
Setting a system property.
It is not supposed to change the config property but facilitate the property, I have to do this inside @BeforeAll method because I'm running MockWebServer which has a dynamic port and I need to append that port.
Something like this:
@BeforeAll
static void init() {
System.setProperty("service.url", "http://localhost:" + mockServer.getPort() + "/api/v1/resource");
}
Here is an example with h2 - embedded-pg (2).zip
JPA repository within test case context unable to find data
As I went back to take some screenshots of the error, I actually found out it was indeed due to different contexts. The repository was only able to grab the table mapped to its entity and the rest of the data (schemas and tables) was sitting in a different context, inaccessible to the repository. I fixed it by annotating the test class as @Transactional. Although, I don't need to do the same in my personal project and it seems to work fine even for non-entity table/function.
Here is the sample - embedded-pg (3).zip
Thanks
Setting a system property
The difference between versions with H2 and Embedded Postgres databases lies in the EmbeddedDatabaseTestExecutionListener
, which evaluates and process the @AutoConfigureEmbeddedDatabase
annotation. This listener causes the Spring context initialization to begin before the @BeforeAll
method is called. Whereas in the case of the H2 database, there is no such listener, and the Spring context initialization occurs after the @BeforeAll
method.
I have the following comments on this:
EmbeddedDatabaseTestExecutionListener
affects tests without the @AutoConfigureEmbeddedDatabase
annotation - this definitely shouldn't happen, so I consider it as a bug and I'm going to fix it as soon as possible.@AutoConfigureEmbeddedDatabase
, this is intended behavior, because evaluating this annotation requires access to the application context, and the annotation must be evaluated at the very beginning of the test start, so before calling the @BeforeAll
method.@BeforeAll
method execution.System.setProperty()
in the @BeforeAll
method (or anywhere else in the test) is an entirely inappropriate way to do this. In your case, it only works because the Spring context is initialized after the @BeforeAll
method is called, but if you have multiple test classes that have the same configuration and share one Spring context, this approach won't work for an existing Spring context. Therefore, I again recommend using annotations like @TestPropertySource
or @DynamicPropertySource
, which are designed for this purpose.JPA repository within test case context unable to find data
All tests are passing normally, I don't see any @Transactional
annotation anywhere. So I don't know what I should be looking into. I need a reproducer that will make it clear what's wrong.
I am using zonky embedded-postgres db for my spring boot integration tests as I have SQL queries with arrow (->) operator which only work with a postgres database.
There are two issues I have observed:
I used @BeforeAll to set a system property, say an API URL
This did not work with zonky's db, it only configured this property when declared in the application-test.properties. I tried the same thing with h2 as the embedded db, both ways, @BeforeAll method and test profile application-test.properties worked fine.
I could resolve it using a static block but I would like to know what makes h2 run it but not zonky.
Here is the sample code - embedded-pg.zip
@Sql(script = "test-data.sql)
This issue occurred in one my organization's project, unfortunately I couldn't replicate it on a personal project. They are using spring-boot-parent-2.5.0 with Java 8.
In a spring boot integration test using
@SpringBootTest
and@AutoConfigureEmbeddedDatabase(type = AutoConfigureEmbeddedDatabase.DatabaseType.POSTGRES, provider = AutoConfigureEmbeddedDatabase.DatabaseProvider.ZONKY)
, I loaded the data into db using @Sql script, verified the data load by enabling debug logs. My test case when trying to run a repository method was unable to find any data or schema for that matter from the query. Interestingly, an @Autowired repository object in the IT test class itself was able to access the db data.For example, testQuery method would fetch data while getDeptTest would say "no such function exists". Note: This is not the behavior of attached code but it did happen on enterprise code with similar configs.
Both of the issues make me think if @AutoConfigureEmbeddedDatabase changes how spring test context loads:
Thanks for your help.