JUnit - @DirtyContext在每个测试方法之后不重置neo4j数据库

时间:2016-12-15 04:44:04

标签: java spring hibernate unit-testing junit

我正在使用Spring 4.3.3.RELEASEHibernate 5.0.11.FinalJUnit 4.1.2SDN4.1.3-RELEASEneo4j-ogm-2.0.5。我阅读并尝试实现@DirtyContext,可以在每个测试方法完成运行后重置我的数据库。但是当我查看我的数据库时,我在Test方法中创建的所有节点仍然存在。

@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`));
    }
}

这就是我如何使用Java Annotation设置配置上下文

@Configuration
public class TestConfig {

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

OrderService是我的接口类,OrderServiceImpl是我的服务实现。我有两个引用,但所有这些都没有用,因为当我查看我的数据库时,所有节点仍然存在,未删除

  1. Reference
  2. Reference

2 个答案:

答案 0 :(得分:1)

您需要将@Transactional注释添加到测试类

@Transactional
public class OrderServiceTest

答案 1 :(得分:0)

您可以使用classMode尝试@DirtiesContext

@DirtiesContext(classMode = BEFORE_EACH_TEST_METHOD)