Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/401.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java SpringIntegrationJPA存储库测试不能正常工作_Java_Spring_Spring Data Jpa_Integration Testing - Fatal编程技术网

Java SpringIntegrationJPA存储库测试不能正常工作

Java SpringIntegrationJPA存储库测试不能正常工作,java,spring,spring-data-jpa,integration-testing,Java,Spring,Spring Data Jpa,Integration Testing,我有件很奇怪的事。我想测试我的存储库级别,我需要测试我是否可以使用相同的用户名保存2个用户用户名字段在数据库中是唯一的。这是我的数据库配置 spring.datasource.driver-class-name=org.postgresql.Driver spring.datasource.url= jdbc:postgresql://localhost:5432/tutorial_test spring.datasource.username=postgres spring.datasourc

我有件很奇怪的事。我想测试我的存储库级别,我需要测试我是否可以使用相同的用户名保存2个用户用户名字段在数据库中是唯一的。这是我的数据库配置

spring.datasource.driver-class-name=org.postgresql.Driver
spring.datasource.url= jdbc:postgresql://localhost:5432/tutorial_test
spring.datasource.username=postgres
spring.datasource.password=root

# General JPA properties
spring.jpa.show-sql=false

#Note: The last two properties on the code snippet above were added to suppress an annoying exception
# that occurs when JPA (Hibernate) tries to verify PostgreSQL CLOB feature.
spring.jpa.database-platform=org.hibernate.dialect.PostgreSQL9Dialect
spring.jpa.properties.hibernate.temp.use_jdbc_metadata_defaults = false

# Hibernate Specific properties
spring.jpa.properties.hibernate.format_sql=false
spring.jpa.hibernate.ddl-auto=create
我的实体类用户:

@Data
@AllArgsConstructor
@NoArgsConstructor
@Entity
@Table(name = "users")
public class User {

    @Id
    @SequenceGenerator(name = "user_id_seq_gen", sequenceName = "user_id_seq", allocationSize = 1)
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "user_id_seq_gen")
    private Long id;
    @Column(nullable = false, unique = true, length = 50)
    @NotNull
    @Length(min = 4, max = 50)
    private String username;
    @Column(nullable = false, length = 50)
    @NotNull
    @Length(min = 6, max = 50)
    private String password;
    @Column(nullable = false, unique = true, length = 100)
    @NotNull
    @Length(min = 6, max = 50)
    private String email;
}
用户存储库:

public interface UserRepository extends JpaRepository<User, Long> {

    boolean existsByUsername(String username);
    boolean existsByEmail(String email);
    User findByUsername(String username);
}
这种测试类的行为非常奇怪。它抛出一个异常,无法保存另一个实体,因为用户名仅在userRepository.deleteAll之后唯一;为什么?好的,如果我删除@After方法,它根本不会抛出异常……而且,如果我添加System.out.printlnuserRepository.findAll;保存第二个用户后,它将出现异常。。。这是怎么回事?? 当我启动应用程序时,所有这些方法都很有效。但在这些集成存储库中,测试出了一些错误。我有另一个repository tests类,在这里我可以用一组未保存的子对象保存父对象,当我从数据库中选择父对象时,它会给我一组ID为空的子对象。。。 我不知道是什么问题,但我想可能是在配置方面??因为我重复一遍,当我启动应用程序时并没有问题,所有的工作都很好。如果您能帮助我,我将非常高兴。

@在您的测试运行之前运行之前。在这里,它插入一个用户名为“Daimon”的用户

@考试就是你的考试。它试图插入一个用户名为“Daimon”的用户,该用户名已存在,因为它是在@Before中插入的。这就是引发异常的原因

@测试结束后运行。它将删除所有用户。

@然后在运行测试之前运行。在这里,它插入一个用户名为“Daimon”的用户

@考试就是你的考试。它试图插入一个用户名为“Daimon”的用户,该用户名已存在,因为它是在@Before中插入的。这就是引发异常的原因


@测试结束后运行。它会删除所有用户。

您看到的是JPA实现方式的副作用。当您持久化或合并一个实体时,大多数人认为它是在保存该实体。这就是为什么调用这些方法的存储库方法被称为save

但是,在第1阶段没有对数据库进行插入。相反,实体只是被跟踪,它将在某些事件中刷新:

事务的提交。 对flush的显式调用。 根据您的配置,可能在查询之前,这里的情况就是这样。 这就是为什么在调用findAll时会出现异常。 deleteAll的实现实际上涉及一个findAll,因此它也会触发异常。
1实际上,根据您的ID生成策略,可能会发生插入。

您看到的是JPA实现方式的副作用。当您持久化或合并一个实体时,大多数人认为它是在保存该实体。这就是为什么调用这些方法的存储库方法被称为save

但是,在第1阶段没有对数据库进行插入。相反,实体只是被跟踪,它将在某些事件中刷新:

事务的提交。 对flush的显式调用。 根据您的配置,可能在查询之前,这里的情况就是这样。 这就是为什么在调用findAll时会出现异常。 deleteAll的实现实际上涉及一个findAll,因此它也会触发异常。
1实际上,根据您的ID生成策略,可能会发生插入。

我知道。但我说过,它在删除表中的所有对象后会抛出这样的异常。如果我删除@After方法,它不会抛出异常。我知道。但我说过,它在删除表中的所有对象后会抛出这样的异常。如果我删除@After方法,它不会抛出异常。是的,谢谢。我已经记得在测试中事务回滚,数据不会刷新。我想回答,但你回答了谢谢谢谢。我已经记得在测试中事务回滚,数据不会刷新。我想回答,但你回答了,谢谢
@RunWith(SpringRunner.class)
@DataJpaTest
@AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.NONE)
@TestPropertySource("/application-test.properties")
public class UserRepositoryTest {

    @Autowired
    private UserRepository userRepository;

    @Before
    public void insertData() {
        user = new User(null, "Daimon", "123123", "krikkk998@mail.ru");
        userRepository.save(user);
    }

    @After
    public void cleanData(){
        user = null;
        userRepository.deleteAll();
    }

    @Test(expected = DataIntegrityViolationException.class)
    public void registerUserWithExistingUsername() {
        val user = new User(null, "Daimon", "123123", "glass999@mail.ru");
        userRepository.save(user);
    }

}