NoSuchElement 执行 mvn 测试后不存在任何值
NoSuchElement No value present after executing mvn test
我开始使用 JUnit 5 为我的服务开发单元测试。
在 运行 测试后我遇到了这些错误:
结果:
测试错误:
WorkSiteRepositoryTest.TestDelete:198 ▒ EmptyResultDataAccess
No class com.xxx.WorkSiteEntity entity with id 5 exists!
WorkSiteRepositoryTest.TestFind:137 ▒ NoSuchElement No value present
WorkSiteRepositoryTest.TestUpdt:161 ▒ NoSuchElement No value present
测试 运行:4,失败:0,错误:3,跳过:0
实体:
// Company name.
@Size (max = 3)
@Column (name="emp", nullable=false, length=4, unique=true)
private Integer companyId = null;
// Work site ID.
@Id
@GeneratedValue (strategy=GenerationType.IDENTITY)
@Column (name="codlug", nullable=false, insertable=false, unique=true, updatable=false)
private Long id = null;
// Full name.
@Size (max = 40)
@Column (name="nombre", nullable=false, length=40)
private String fullName = null;
// Short name.
@Size (max = 15)
@Column (name="nomabre", nullable=false, length=15)
private String shortName = null;
// Country code.
@Size (max = 3)
@Column (name="pais", nullable=true, length=3)
private String countryCode = null;
// Province code.
@Size (max = 3)
@Column (name="pcia", nullable=true, length=3)
private String provinceCode = null;
// Province zip code.
@Size (max = 8)
@Column (name="codp", nullable=true, length=8)
private String zipCode = null;
// Work site address.
@Size (max = 40)
@Column (name="direc", nullable=false, length=40)
private String address = null;
// Building site number.
@Size (max = 9)
@Column (name="nroobra", nullable=true, length=9)
private Integer buildingNumber = null;
// Site's title
@Size (max = 4)
@Column (name="titlug", nullable=true, length=4)
private Integer siteTitle = null;
// Status
@Size (max = 1)
@Column (name="status", nullable=true, length=1)
private Integer status = null;
// Active
@Size (max = 4)
@Column (name="activ", nullable=true, length=4)
private Integer active = null;
// Is done
@Size (max = 1)
@Column (name="realiz", nullable=true, length=1)
private Integer done = null;
// Exoneration
@Size (max = 3)
@Column (name="exoneracion", nullable=true, length=3)
private Integer exoneration = null;
// Address number
@Size (max = 6)
@Column (name="domnro", nullable=true, length=2)
private Integer addressNumber = null;
// Office number
@Size (max = 6)
@Column (name="domofi", nullable=true, length=4)
private String officeNumber = null;
// Phone number (1)
@Size (max = 25)
@Column (name="tel1", nullable=true, length=25)
private String phoneNumber = null;
// Fax number
@Size (max = 25)
@Column (name="fax", nullable=true, length=25)
private String fax = null;
单元测试
使用的代码:
@Autowired
private IWorkSiteRepository workSiteRepository;
public WorkSiteRepositoryTest() {
super ();
}
@BeforeEach
public void setup() {
List<WorkSiteEntity> workSite = Arrays.asList (
new WorkSiteEntity (1,
"testname", "aa", "aa", "aa", "1100",
"testaddress", null, null, null, null, null, null, null,
"9", "2342", "326"),
new WorkSiteEntity (2,
"testname", "aa", "aa", "aa", "1200",
"testaddress", null, null, null, null, null, null, null,
"9", "623", "436"),
new WorkSiteEntity (3,
"testname", "aa", "aa", "aa", "1300",
"testaddress", null, null, null, null, null, null, null,
"9", "342", "567"),
new WorkSiteEntity (4,
"testname", "aa", "aa", "aa", "1400",
"testaddress", null, null, null, null, null, null, null,
"9", "543", "8567"),
new WorkSiteEntity (5,
"testname", "aa", "aa", "aa", "1900",
"testaddress", null, null, null, null, null, null, null,
"9", "7754", "885")
);
this.workSiteRepository.saveAll(workSite);
}
@AfterEach
public void release() {
this.workSiteRepository.deleteAll();
}
@Test
@DisplayName ("Find a WorkSite Entity By Id.")
public void TestFind () {
WorkSiteEntity workSite;
Long id = Long.valueOf(1500); // PK to search.
workSite = this.workSiteRepository.findById(id).get(); // This is line 137
assertNotNull(workSite, "[FINDBY] WorkSite " + id + " not found!");
}
@Test
@DisplayName ("Update a WorkSite Entity By Id.")
public void TestUpdt () {
WorkSiteEntity workSite;
Long id = Long.valueOf(1777); // PK to search.
workSite = this.workSiteRepository.findById(id).get();
assertNotNull (workSite, "[UPDATE] WorkSite " + id + " not found!");
String before = workSite.getFullName ();
workSite.setFullName (before.toUpperCase());
this.workSiteRepository.saveAndFlush(workSite);
workSite = this.workSiteRepository.findById(id).get(); // This is line 161
assertNotNull (workSite, "[UPDATE] WorkSite not " + id + " found!");
assertEquals (before.toUpperCase (), workSite.getFullName (), "[UPDATE] Update process failed!");
}
@Test
@DisplayName ("Delete a WorkSite Entity By Id.")
public void TestDelete () {
WorkSiteEntity workSite;
Long id = Long.valueOf(5); // PK to search.
this.workSiteRepository.deleteById(id); // This is line 198
workSite = this.workSiteRepository.findById(id).orElse(null);
assertNull (workSite, "[DELETE] WorkSite " + id + " found!");
}
@Test
@DisplayName ("Find all WorkSites")
public void TestFindAll () {
List<WorkSiteEntity> workSite;
workSite = this.workSiteRepository.findAll();
assertNotNull (workSite, "[FINDALL] WorkSite not found (NULL)!");
assertFalse (workSite.isEmpty (), "[FINDALL] WorkSite not found (Empty)!");
}
为什么我收到无值错误?
此外,错误显示没有 ID 5,但我将该 ID 添加到对象
我已经评论了显示错误的行
问题: 与主要字段定义有关。尽管您要删除 @AfterEach
方法中的所有记录,但每次测试的身份都会不断增加,因此第一个测试将从 1..5 的 seq 开始,第二个测试从 6..10 开始,然后继续为什么它没有通过 id 找到实体。
@Id
@GeneratedValue (strategy=GenerationType.IDENTITY)
@Column (name="codlug", nullable=false, insertable=false, unique=true, updatable=false)
private Long id = null;
参考:
结果:
注意:由于主值不匹配,以下方法将不起作用。
@Test
@DisplayName ("Find a WorkSite Entity By Id.")
public void TestFind () {
Long id = Long.valueOf(1500); // PK to search.
}
@Test
@DisplayName ("Update a WorkSite Entity By Id.")
public void TestUpdt () {
Long id = Long.valueOf(1777); // PK to search.
}
使用可选检查元素是否存在。
我开始使用 JUnit 5 为我的服务开发单元测试。
在 运行 测试后我遇到了这些错误:
结果:
测试错误:
WorkSiteRepositoryTest.TestDelete:198 ▒ EmptyResultDataAccess
No class com.xxx.WorkSiteEntity entity with id 5 exists!
WorkSiteRepositoryTest.TestFind:137 ▒ NoSuchElement No value present
WorkSiteRepositoryTest.TestUpdt:161 ▒ NoSuchElement No value present
测试 运行:4,失败:0,错误:3,跳过:0
实体:
// Company name.
@Size (max = 3)
@Column (name="emp", nullable=false, length=4, unique=true)
private Integer companyId = null;
// Work site ID.
@Id
@GeneratedValue (strategy=GenerationType.IDENTITY)
@Column (name="codlug", nullable=false, insertable=false, unique=true, updatable=false)
private Long id = null;
// Full name.
@Size (max = 40)
@Column (name="nombre", nullable=false, length=40)
private String fullName = null;
// Short name.
@Size (max = 15)
@Column (name="nomabre", nullable=false, length=15)
private String shortName = null;
// Country code.
@Size (max = 3)
@Column (name="pais", nullable=true, length=3)
private String countryCode = null;
// Province code.
@Size (max = 3)
@Column (name="pcia", nullable=true, length=3)
private String provinceCode = null;
// Province zip code.
@Size (max = 8)
@Column (name="codp", nullable=true, length=8)
private String zipCode = null;
// Work site address.
@Size (max = 40)
@Column (name="direc", nullable=false, length=40)
private String address = null;
// Building site number.
@Size (max = 9)
@Column (name="nroobra", nullable=true, length=9)
private Integer buildingNumber = null;
// Site's title
@Size (max = 4)
@Column (name="titlug", nullable=true, length=4)
private Integer siteTitle = null;
// Status
@Size (max = 1)
@Column (name="status", nullable=true, length=1)
private Integer status = null;
// Active
@Size (max = 4)
@Column (name="activ", nullable=true, length=4)
private Integer active = null;
// Is done
@Size (max = 1)
@Column (name="realiz", nullable=true, length=1)
private Integer done = null;
// Exoneration
@Size (max = 3)
@Column (name="exoneracion", nullable=true, length=3)
private Integer exoneration = null;
// Address number
@Size (max = 6)
@Column (name="domnro", nullable=true, length=2)
private Integer addressNumber = null;
// Office number
@Size (max = 6)
@Column (name="domofi", nullable=true, length=4)
private String officeNumber = null;
// Phone number (1)
@Size (max = 25)
@Column (name="tel1", nullable=true, length=25)
private String phoneNumber = null;
// Fax number
@Size (max = 25)
@Column (name="fax", nullable=true, length=25)
private String fax = null;
单元测试 使用的代码:
@Autowired
private IWorkSiteRepository workSiteRepository;
public WorkSiteRepositoryTest() {
super ();
}
@BeforeEach
public void setup() {
List<WorkSiteEntity> workSite = Arrays.asList (
new WorkSiteEntity (1,
"testname", "aa", "aa", "aa", "1100",
"testaddress", null, null, null, null, null, null, null,
"9", "2342", "326"),
new WorkSiteEntity (2,
"testname", "aa", "aa", "aa", "1200",
"testaddress", null, null, null, null, null, null, null,
"9", "623", "436"),
new WorkSiteEntity (3,
"testname", "aa", "aa", "aa", "1300",
"testaddress", null, null, null, null, null, null, null,
"9", "342", "567"),
new WorkSiteEntity (4,
"testname", "aa", "aa", "aa", "1400",
"testaddress", null, null, null, null, null, null, null,
"9", "543", "8567"),
new WorkSiteEntity (5,
"testname", "aa", "aa", "aa", "1900",
"testaddress", null, null, null, null, null, null, null,
"9", "7754", "885")
);
this.workSiteRepository.saveAll(workSite);
}
@AfterEach
public void release() {
this.workSiteRepository.deleteAll();
}
@Test
@DisplayName ("Find a WorkSite Entity By Id.")
public void TestFind () {
WorkSiteEntity workSite;
Long id = Long.valueOf(1500); // PK to search.
workSite = this.workSiteRepository.findById(id).get(); // This is line 137
assertNotNull(workSite, "[FINDBY] WorkSite " + id + " not found!");
}
@Test
@DisplayName ("Update a WorkSite Entity By Id.")
public void TestUpdt () {
WorkSiteEntity workSite;
Long id = Long.valueOf(1777); // PK to search.
workSite = this.workSiteRepository.findById(id).get();
assertNotNull (workSite, "[UPDATE] WorkSite " + id + " not found!");
String before = workSite.getFullName ();
workSite.setFullName (before.toUpperCase());
this.workSiteRepository.saveAndFlush(workSite);
workSite = this.workSiteRepository.findById(id).get(); // This is line 161
assertNotNull (workSite, "[UPDATE] WorkSite not " + id + " found!");
assertEquals (before.toUpperCase (), workSite.getFullName (), "[UPDATE] Update process failed!");
}
@Test
@DisplayName ("Delete a WorkSite Entity By Id.")
public void TestDelete () {
WorkSiteEntity workSite;
Long id = Long.valueOf(5); // PK to search.
this.workSiteRepository.deleteById(id); // This is line 198
workSite = this.workSiteRepository.findById(id).orElse(null);
assertNull (workSite, "[DELETE] WorkSite " + id + " found!");
}
@Test
@DisplayName ("Find all WorkSites")
public void TestFindAll () {
List<WorkSiteEntity> workSite;
workSite = this.workSiteRepository.findAll();
assertNotNull (workSite, "[FINDALL] WorkSite not found (NULL)!");
assertFalse (workSite.isEmpty (), "[FINDALL] WorkSite not found (Empty)!");
}
为什么我收到无值错误? 此外,错误显示没有 ID 5,但我将该 ID 添加到对象
我已经评论了显示错误的行
问题: 与主要字段定义有关。尽管您要删除 @AfterEach
方法中的所有记录,但每次测试的身份都会不断增加,因此第一个测试将从 1..5 的 seq 开始,第二个测试从 6..10 开始,然后继续为什么它没有通过 id 找到实体。
@Id
@GeneratedValue (strategy=GenerationType.IDENTITY)
@Column (name="codlug", nullable=false, insertable=false, unique=true, updatable=false)
private Long id = null;
参考:
结果:
注意:由于主值不匹配,以下方法将不起作用。
@Test
@DisplayName ("Find a WorkSite Entity By Id.")
public void TestFind () {
Long id = Long.valueOf(1500); // PK to search.
}
@Test
@DisplayName ("Update a WorkSite Entity By Id.")
public void TestUpdt () {
Long id = Long.valueOf(1777); // PK to search.
}
使用可选检查元素是否存在。