繁体   English   中英

JUnit-@DirtyContext在每种测试方法后未重置neo4j数据库

[英]JUnit - @DirtyContext not reset neo4j database after each test method

我正在使用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注释设置配置上下文的方式

@Configuration
public class TestConfig {

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

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

  1. 参考
  2. 参考

您需要在测试类中添加@Transactional批注

@Transactional
public class OrderServiceTest

您可以使用classMode尝试@DirtiesContext

@DirtiesContext(classMode = BEFORE_EACH_TEST_METHOD)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM