79778144

Date: 2025-09-29 13:21:23
Score: 1
Natty:
Report link

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(...);
    }
}
Reasons:
  • Long answer (-0.5):
  • Has code block (-0.5):
  • User mentioned (1): @Transactional
  • Low reputation (1):
Posted by: Lavi Gupta