我有一个包含两个方法测试的测试类。这些测试方法使用@order注解按一定顺序执行,效果很好。第一个方法在h2中插入一个id=21的已知值(请相信我,在spring启动时,我在spring应用程序配置类@springbootplication中创建了20个值),下面是测试层中插入的值:
user userstub=新用户(“prenom21”,“nom21”))
第二个方法必须读取插入的值,但我得到一个空对象。也许我做错了什么,但我想知道是什么。
脚本可在此处查看或下载(您可以运行它查看错误):https://github.com/userinterview/loizspringrestpersistencetests/blob/main/src/test/java/org/loiz/demo/loizpersistencetest.java
下面,在用粗体写的代码行(第65行),我应该在第一个测试方法创建的userread对象中有一个对象,但是我有null对象。这正常吗?
@Test
@Order(2)
@DisplayName("Test de suppression du user \"prenom21 Nom21\"")
public void readShouldMapCorrectly() throws Exception {
User userStub = new User(idStub, "prenom21", "Nom21");
User UserRead = this.testEntityManager.find(User.class, idStub) ;
Assert.assertTrue(userStub.equals(UserRead));
}
它不应为null,因为第一个方法在h2中插入/创建该记录。见下表:
package org.loiz.demo;
import org.assertj.core.api.BDDAssertions;
import org.junit.Assert;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.MethodOrderer;
import org.junit.jupiter.api.Test ;
import org.junit.jupiter.api.TestMethodOrder;
import org.junit.jupiter.api.Order ;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
import org.springframework.boot.test.autoconfigure.orm.jpa.TestEntityManager;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringRunner;
import demo.LoizBootSpringDemoApplication;
import demo.crepository.UserRepositoryInterface;
import demo.dmodel.User;
//Test de la couche de persistence
@RunWith(SpringRunner.class)
@DataJpaTest
@ContextConfiguration(classes = { LoizBootSpringDemoApplication.class})
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
public class LoizPersistenceTest
{
@Autowired
private TestEntityManager testEntityManager;
@Autowired
private UserRepositoryInterface repository;
private static Long idStub ;
@Test
@Order(1)
@DisplayName("Test de sauvegarde d\'un user \"prenom21 Nom21\"")
public void saveShouldMapCorrectly() throws Exception {
User userStub = new User("prenom21", "Nom21");
User UserSaved = this.testEntityManager.persistFlushFind(userStub);
BDDAssertions.then(UserSaved.getId()).isNotNull();
idStub = UserSaved.getId() ;
User UserRead = this.testEntityManager.find(User.class, idStub) ;
BDDAssertions.then(UserSaved.getFirstName()).isNotBlank();
BDDAssertions.then(UserSaved.getFirstName()).isEqualToIgnoringCase("prenom21");
BDDAssertions.then(UserSaved.getLastName()).isEqualToIgnoringCase("Nom21");
BDDAssertions.then(UserSaved.getLastName()).isNotBlank();
}
@Test
@Order(2)
@DisplayName("Test de suppression du user \"prenom21 Nom21\"")
public void readShouldMapCorrectly() throws Exception {
User userStub = new User(idStub, "prenom21", "Nom21");
User UserRead = this.testEntityManager.find(User.class, idStub) ;
Assert.assertTrue(userStub.equals(UserRead));
}
}
我很想了解一下这个问题:)
1条答案
按热度按时间ie3xauqp1#
在运行这些测试时,spring在每个测试方法之后执行完全回滚。在执行写操作的测试中,添加
从
org.springframework.test.annotation.Rollback;