Mark your setup method or helper as not transactional, so it persists data once and survives across test methods:
Here the trick is: don’t put @Transactional on the class. Instead, only annotate test methods that need rollback.
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { ... })
public class DatabaseFilterTest {
private static boolean setUpIsDone = false;
@Autowired
private YourRepository repo;
@Before
public void setUp() {
if (!setUpIsDone) {
insertTestData(); // persists real data, not rolled back
setUpIsDone = true;
}
}
@Transactional // applied only at method level if needed
@Test
public void testSomething() {
...
}
@Rollback(false) // ensures data is committed
@Test
public void insertTestData() {
repo.save(...);
}
}