java 如何在测试之间重置
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/41092716/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to reset between tests
提问by robert trudel
I have a test class
我有一个测试班
@RunWith(SpringRunner.class)
@DataJpaTest
I have two tests. In every test I do the same operation, persist the object. Only the find call are different.
我有两个测试。在每次测试中我都做同样的操作,持久化对象。只有 find 调用不同。
If I run both tests together they fail but if I run test one after another they are successful.
如果我同时运行两个测试,它们会失败,但如果我一个接一个地运行测试,它们就会成功。
There is no reset between each test. How to do that? Only the call to the repository is different in each test.
每次测试之间没有重置。怎么做?每个测试中只有对存储库的调用不同。
@Test
public void findTopByCommerceCommerceIdOrderByEntryTimeDesc() {
Long commerceId = 1L;
Commerce commerce = new Commerce();
commerce.setName("test");
this.entityManager.persist(commerce);
Member member = new Member();
member.setCommerce(commerce);
member.setMan(true);
member.setName("bob binette");
this.entityManager.persist(member);
Visit visit1 = new Visit();
visit1.setCommerce(commerce);
visit1.setMember(member);
visit1.setEntryTime(LocalDateTime.of(LocalDate.now(), LocalTime.now()));
Visit visit2 = new Visit();
visit2.setCommerce(commerce);
visit2.setMember(member);
visit2.setEntryTime(LocalDateTime.of(LocalDate.now().minusDays(2), LocalTime.now()));
this.entityManager.persist(visit1);
this.entityManager.persist(visit2);
Visit visit = visitRepository.findTopByCommerceCommerceIdOrderByEntryTimeDesc(commerceId);
assertEquals(visit.getVisitId(), Long.valueOf("1"));
}
Edit
编辑
i put all the code : http://pastebin.com/M9w9hEYQ
我把所有的代码:http: //pastebin.com/M9w9hEYQ
回答by HPacquee
Add the @DirtiesContext annotation, but provide it with the AFTER_EACH_TEST_METHODclassMode
添加@DirtiesContext 注释,但为其提供AFTER_EACH_TEST_METHODclassMode
@DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_EACH_TEST_METHOD)
回答by marok
In your case for each test you persist the same data. So you should persist data before all test or persist before each test and clean after it.
在您的情况下,对于每个测试,您都保留相同的数据。因此,您应该在所有测试之前保留数据,或者在每次测试之前保留数据并在测试之后进行清理。
1. Persist before all test
1.坚持在所有测试之前
@BeforeClass
public static void init(){
//persist your data
}
@AfterClass
public static void clear(){
//remove your data
}
@Test
public void findTopByCommerceCommerceIdOrderByEntryTimeDesc() {
Visit visit = visitRepository.findTopByCommerceCommerceIdOrderByEntryTimeDesc(commerceId);
assertEquals(visit.getVisitId(), Long.valueOf("1"));
}
In this case @AfterClass is optionally
在这种情况下,@AfterClass 是可选的
2. Persist before each test and clean after each test
2.每次测试前坚持,每次测试后清洁
@Before
public void init(){
//persist your data
}
@After
public void clear(){
//remove your data
}
@Test
public void findTopByCommerceCommerceIdOrderByEntryTimeDesc() {
Visit visit = visitRepository.findTopByCommerceCommerceIdOrderByEntryTimeDesc(commerceId);
assertEquals(visit.getVisitId(), Long.valueOf("1"));
}
Remember that methods which use @BeforeClass and @AfterClass must be static.
请记住,使用@BeforeClass 和@AfterClass 的方法必须是静态的。
回答by nesohc
You can use @DirtiesContext annotation on your test class to reset the tests, there you can also choose when to reset. Default is after every method, but you can change that by passing in different parameters to the @DirtiesContext annotation.
您可以在测试类上使用 @DirtiesContext 注释来重置测试,您还可以选择何时重置。默认值在每个方法之后,但您可以通过将不同的参数传递给 @DirtiesContext 注释来更改它。
import org.springframework.test.annotation.DirtiesContext;
@RunWith(SpringRunner.class)
@DataJpaTest
@DirtiesContext
public class VisitRepositoryTest {
回答by Ruchi Saini
Use @Sql with ExecutionPhase.AFTER_TEST_METHOD and pass the script that is to be used to clean the database
将@Sql 与 ExecutionPhase.AFTER_TEST_METHOD 一起使用并传递用于清理数据库的脚本
@Sql(scripts="classpath:cleanup.sql",executionPhase=Sql.ExecutionPhase.AFTER_TEST_METHOD)
@Test
public void whateverIsYourTestMethod()
{
...
}
In case you are using @Transactional annotation , you can use :
如果您使用 @Transactional 注释,您可以使用:
@Sql(scripts="classpath:cleanup.sql",executionPhase=Sql.ExecutionPhase.AFTER_TEST_METHOD,config = @SqlConfig
( transactionMode = TransactionMode.ISOLATED,
transactionManager = "transactionManager",
dataSource= "dataSource" ))
@Test
@Commit
@Transactional
public void whateverIsYourTestMethod(){...}
回答by Xephi
Have you try to clear the peristence cache between each tests, according to TestEntityManager#clear()
根据TestEntityManager#clear(),您是否尝试清除每个测试之间的持久性缓存
@After
public void clear() {
this.entityManager.clear();
}
Or maybe try to set your Visitor
as fields and remove them in an after, than flush the changes :
或者尝试设置您的Visitor
as 字段并在之后删除它们,而不是刷新更改:
Visit visit1;
Visit visit2;
@After
public void clear(){
if (visit1 != null)
this.entityManager.remove(visit1);
if (visit2 != null)
this.entityManager.remove(visit2);
this.entityManager.flush();
}
回答by kle pra
@DirtiesContext(classMode =DirtiesContext.ClassMode.AFTER_EACH_TEST_METHOD
)
works for me, but it is only necessary when I run @DataJpaTest()
with @AutoConfigureTestDatabase(replace=Replace.NONE)
and costum profile for mysql, default h2 works without this.
@DirtiesContext(classMode =DirtiesContext.ClassMode.AFTER_EACH_TEST_METHOD
) 对我有用,但只有在我@DataJpaTest()
使用@AutoConfigureTestDatabase(replace=Replace.NONE)
mysql 和 costum 配置文件运行时才有必要,默认 h2 没有这个就可以工作。