我正在使用Spring 4.3.3.RELEASE
,Hibernate 5.0.11.Final
,JUnit 4.1.2
,SDN4.1.3-RELEASE
和neo4j-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
是我的服务实现。我有两个引用,但所有这些都没有用,因为当我查看我的数据库时,所有节点仍然存在,未删除
答案 0 :(得分:1)
您需要将@Transactional
注释添加到测试类
@Transactional
public class OrderServiceTest
答案 1 :(得分:0)
您可以使用classMode尝试@DirtiesContext
@DirtiesContext(classMode = BEFORE_EACH_TEST_METHOD)