Description
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:
- Setting a system property.
I used @BeforeAll to set a system property, say an API URL
@BeforeAll
static void init() {
System.setProperty("service.url", "http://localhost:8080/api/v1/resource");
}
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
- JPA repository within test case context unable to find data loaded from
@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.
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@Sql("/test-schema.sql")
@ActiveProfiles("test")
@AutoConfigureEmbeddedDatabase(type = AutoConfigureEmbeddedDatabase.DatabaseType.POSTGRES, provider = AutoConfigureEmbeddedDatabase.DatabaseProvider.ZONKY)
public class DepartmentControllerIT {
private static final String api = "http://localhost:%d/departments/get/1";
private String url;
@LocalServerPort
private int port;
@Autowired
private TestRestTemplate restTemplate;
@Autowired
private DepartmentRepository departmentRepository;
@BeforeAll
static void init() {
System.setProperty("service.url", "someUrl");
}
@BeforeEach
void setUp() {
url = String.format(api, port);
}
@Test
void testQuery() {
assertThat(departmentRepository.getId()).isEqualTo("0123456789");
}
@Test
void getDeptTest() {
Department actual = restTemplate.getForObject(url, Department.class);
System.out.println(actual);
assertThat(actual).isNotNull();
}
}
Both of the issues make me think if @AutoConfigureEmbeddedDatabase changes how spring test context loads:
- In the first issue, context only recognizes the test profile properties and doesn't bother to reach @BeforeAll and fails.
- Second issue is rather interesting; I couldn't figure out what happens to the repository objects, the one in IT class is able to access the data while the one in test case flow doesn't.
Thanks for your help.