我正在udemy上学习Spring 5等,我在测试我们的应用程序的部分。到现在为止一切都很好,我能够连接到PostgreSQL数据库,但现在我被困在这个测试失败,因为2天。
我不明白是什么导致测试失败。应用程序运行,但测试没有。下面是测试类:
package com.ghevi.dao;
import com.ghevi.pma.ProjectManagementApplication;
import com.ghevi.pma.dao.ProjectRepository;
import com.ghevi.pma.entities.Project;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.jdbc.Sql;
import org.springframework.test.context.jdbc.SqlGroup;
import org.springframework.test.context.junit4.SpringRunner;
import static org.junit.Assert.assertEquals;
@ContextConfiguration(classes= ProjectManagementApplication.class)
@RunWith(SpringRunner.class)
@DataJpaTest // for temporary databases like h2
@SqlGroup({
@Sql(executionPhase = Sql.ExecutionPhase.BEFORE_TEST_METHOD, scripts = {"classpath:schema.sql", "classpath:data.sql"}),
@Sql(executionPhase = Sql.ExecutionPhase.AFTER_TEST_METHOD, scripts = "classpath:drop.sql")
})
public class ProjectRepositoryIntegrationTest {
@Autowired
ProjectRepository proRepo;
@Test
public void ifNewProjectSaved_thenSuccess(){
Project newProject = new Project("New Test Project", "COMPLETE", "Test description");
proRepo.save(newProject);
assertEquals(5, proRepo.findAll().size());
}
}
下面是堆栈跟踪:
https://pastebin.com/WcjNU76p
员工类(不要介意这些评论,它们可能是垃圾):
package com.ghevi.pma.entities;
import javax.persistence.*;
import java.util.List;
@Entity
public class Employee {
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "employee_seq") // AUTO for data insertion in the class projmanagapplication (the commented out part), IDENTITY let hibernate use the database id counter.
private long employeeId; // The downside of IDENTITY is that if we batch a lot of employees or projects it will be much slower to update them, we use SEQUENCE now that we have schema.sql (spring does batch update)
private String firstName;
private String lastName;
private String email;
// @ManyToOne many employees can be assigned to one project
// Cascade, the query done on projects it's also done on children entities
@ManyToMany(cascade = {CascadeType.DETACH, CascadeType.MERGE, CascadeType.REFRESH, CascadeType.PERSIST}, // Standard in the industry, dont use the REMOVE (if delete project delete also children) or ALL (because include REMOVE)
fetch = FetchType.LAZY) // LAZY is industry standard it loads project into memory, EAGER load also associated entities so it slows the app, so we use LAZY and call child entities later
//@JoinColumn(name="project_id") // Foreign key, creates a new table on Employee database
@JoinTable(name = "project_employee", // Merge the two table using two foreign keys
joinColumns = @JoinColumn(name="employee_id"),
inverseJoinColumns = @JoinColumn(name="project_id"))
private List<Project> projects;
public Employee(){
}
public Employee(String firstName, String lastName, String email) {
this.firstName = firstName;
this.lastName = lastName;
this.email = email;
}
public List<Project> getProjects() {
return projects;
}
public void setProjects(List<Project> projects) {
this.projects = projects;
}
/* Replaced with List<Project>
public Project getProject() {
return project;
}
public void setProject(Project project) {
this.project = project;
}
*/
public long getEmployeeId() {
return employeeId;
}
public void setEmployeeId(long employeeId) {
this.employeeId = employeeId;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
}
这也是schema.sql
,我在这里引用了这些序列,因为这个文件是由测试运行的,我刚刚注意到IntelliJ在这个文件中标记了一些错误。例如,它将一些空格和TABLE的T标记为红色,说:
第一次
4条答案
按热度按时间tyu7yeag1#
你永远不会告诉它序列,只是发生器叫什么
尝试
stszievb2#
我遇到了同样的问题。添加下面的注解解决了这个问题。
@SequenceGenerator(name = "employee_seq", allocationSize = 1)
wgeznvg73#
可能是employee实体中的生成器定义有问题。“generator”必须是SequenceGenerator的“name”,而不是序列等其他内容的名称。可能是因为您提供了序列的名称,并且没有具有该名称的生成器,所以它使用了默认的预分配,即50。
此外,策略应为SEQUENCE,但如果定义了生成器,则不需要该策略,仅当未定义生成器时,该策略才相关。
jfgube3f4#
默认情况下,SequenceGenerator中的
allocationSize
参数设置为50。当序列增量不匹配时会出现此问题。您可以根据需要更改序列增量值或分配allocationSize大小。