1

I'm using Spring 4.3.3.RELEASE, Hibernate 5.0.11.Final, JUnit 4.1.2, SDN4.1.3-RELEASE and neo4j-ogm-2.0.5. I read and try to implement @DirtyContext that can reset my Database after each test method finish running. But When I look at my Database, All nodes that i created in Test method still there.

@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = MockApplication.class)
@ContextConfiguration(classes = TestConfig.class)
@DirtiesContext(classMode=ClassMode.AFTER_EACH_TEST_METHOD)
public class OrderServiceTest {

    @Autowired
    private OrderService orderService;

    @Autowired
    private CustomerService customerService;

    @Autowired
    private TableService tableService;

    @Test
    public void orderMenuFoodAndDrink() throws Exception {
        Customer customer = new Customer();
        customer.setName("Customer 1");
        customerService.save(customer);

        Table table = new Table ();
        table.setCustomer(customer);
        tableService.save(table);

        ObjectResult result = orderService.orderTable(customer.getId());

        assertThat(result.getTables().get(0), is(table`enter code here`));
    }
}

And this is How I set my Configuration Context with Java Annotation

@Configuration
public class TestConfig {

    @Primary
    @Bean
    public OrderService OrderService() {
        OrderService OrderService = new OrderServiceImpl();
        return OrderService;
    }
}

OrderService is my Interface Class, and OrderServiceImpl is my Service Implementation. I have two references, but all of those is not working, because when I look at my database, all nodes still there, not deleted

  1. Reference
  2. Reference
Community
  • 1
  • 1
David Vincent
  • 634
  • 1
  • 8
  • 33

2 Answers2

1

You need to add @Transactional annotation to your test class

@Transactional
public class OrderServiceTest
Jobin
  • 5,610
  • 5
  • 38
  • 53
1

You can try @DirtiesContext with classMode

@DirtiesContext(classMode = BEFORE_EACH_TEST_METHOD)
Vadim Dissa
  • 961
  • 3
  • 13
  • 32