【发布时间】:2020-12-15 23:04:17
【问题描述】:
我有一个包含两个方法测试的测试类。 这些测试方法使用@Order 注释按一定的顺序执行,效果很好。 第一种方法在 h2 中插入一个已知值,其 id = 21(相信我,在启动时,我在 spring 启动时创建了 20 个值,在注释为 @SpringBootApplication 的 spring 应用程序配置类中),这是插入的值测试层:
用户 userStub = new User("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)); }
- 它不应为空,因为第一个方法在 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));
}
}
我希望能对这个问题有所了解:)
【问题讨论】:
标签: java spring unit-testing testing spring-data