Reputation: 1277
I am running a JUnit integration test with an in-memory H2 database configured like so:
@Bean
public DataSource dataSource() {
try {
SimpleDriverDataSource simpleDriverDataSource = new SimpleDriverDataSource();
simpleDriverDataSource.setDriverClass((Class<? extends Driver>) ClassUtils.forName("org.h2.Driver", this.getClass().getClassLoader()));
simpleDriverDataSource.setUrl("jdbc:h2:file:testdb;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=false;MVCC=true;FILE_LOCK=NO;mv_store=false");
simpleDriverDataSource.setUsername("sa");
simpleDriverDataSource.setPassword("");
return simpleDriverDataSource;
} catch(ClassNotFoundException e) {
throw new IllegalStateException(e.getMessage());
}
}
The test makes a call to a service method. This service method uses an executor service to fork the processing. Before the call to the service class, the test method inserts some data into the database, and this data can be read using the service (via JPA repository calls) up until the task is submitted to the executor service. However, when the task is run, it is unable to read the data that was persisted previously.
How can I make the forked thread see the persisted data?
Note: This is only failing for the Unit test, it is working fine at runtime.
Upvotes: 7
Views: 3947
Reputation: 6058
Assuming that your test is @Transactional
, what you see is the expected behaviour as you (usually) don't want the changes made by a test to be visible to other tests/threads, and possibly, rolled back at the end of the test.
A simple way to work around this problem is to annotate the @Test
method like this
@Test
@Transactional(propagation = Propagation.NEVER)
public void testYourLogics() { /* ... */ }
I've been able to reproduce tour behaviour without the propagation = Propagation.NEVER
and to see it solved with it.
Please note that you will have to restore the database to a clean state manually after running the test, or the changes made by that test, will be visible to the other tests
Upvotes: 8