java @Size测试不会引发数据完整性违规异常

83qze16e  于 2023-02-14  发布在  Java
关注(0)|答案(1)|浏览(199)

我尝试在下面的类上测试@Size注解,我希望在使用仓库"保存"名称长度小于3的主题时,测试会抛出一个异常。问题是没有抛出任何异常。
类别:

@Entity 
@Builder 
@Data 
@AllArgsConstructor 
@NoArgsConstructor
@Table(name = "topic")
public class TopicEntity implements Serializable {
    
    @Id @GeneratedValue( strategy = GenerationType.IDENTITY )
    private Long id;

    @Column( length=200, unique = true, nullable = false )
    @Size(min = 3, max = 200, message 
      = "Topic name must be between 3 and 200 characters")
    @NotBlank
    private String name;

    @Builder.Default
    @OneToMany( mappedBy="theTopic", cascade = CascadeType.MERGE )
    List<QuestionEntity> questions =  new ArrayList<>();

    @Builder.Default
    @OneToMany( mappedBy="theTopic", cascade = CascadeType.MERGE )
    List<Source> sources =  new ArrayList<>();
}

测试,它实际上是在一个测试类文件中,根据你们其中一个人的请求,我将包括完整的类代码..看起来@Size注解与数据库约束无关,请让我知道应用/测试此类验证的一些好做法

@DataJpaTest
public class TopicRepositoryTest {

@Autowired
TopicRepository repository;

TopicEntity topicA, topicB;

@BeforeEach
public void setUp(){
    topicA = TopicEntity.builder()
        .name("Java")
        .build();
    
    topicB = TopicEntity.builder()
        .name("Java")
        .build();

}


    @Test
    void giventopicsWithSameName_whenSaveThem_thenThrowException(){
        repository.save(topicA);

        assertThrows(DataIntegrityViolationException.class,
            () -> repository.save(topicB));

    }

    @Test
    void givenNullTopicName_whenSaveTopic_thenThrowException(){
        TopicEntity topic = TopicEntity.builder()
            .build();
        assertThrows(DataIntegrityViolationException.class,
            () -> repository.save(topic));

    }

     @Test
    void givenShortTopicName_whenSaveTopic_thenThrowException(){
        TopicEntity topic = TopicEntity.builder()
            .name("")
            .build();

        assertThrows(DataIntegrityViolationException.class,
            () -> repository.save(topic));

    }
}

储存库:

package com.learning.javainterviewquestions.repositories;
import org.springframework.data.jpa.repository.JpaRepository;
import com.learning.javainterviewquestions.entities.TopicEntity;

public interface TopicRepository extends  JpaRepository < TopicEntity, Long >{

    TopicEntity findByName(String topic);
    
}

POM:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>3.0.1</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.learning</groupId>
    <artifactId>java-interview-questions</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>java-interview-questions</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>17</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-hateoas</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-data-jpa -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-jpa</artifactId>
    <version>3.0.1</version>
</dependency>

<dependency>
    <groupId>com.h2database</groupId>
    <artifactId>h2</artifactId>
    <scope>test</scope>
</dependency>

<dependency>
    <groupId>org.hibernate</groupId>
     <artifactId>hibernate-validator</artifactId>
    <version>6.0.12.Final</version>
</dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <excludes>
                        <exclude>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                        </exclude>
                    </excludes>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>
dced5bon

dced5bon1#

当使用@DataJpaTest时,每个测试都在一个事务内执行(-〉JavaDoc表示注解包括@Transactional注解)。这意味着,除非使用.saveAndFlush方法或直接使用EntityManager刷新更改,否则数据不会实际刷新到DB。

1.使用.saveAndFlush

2.使用@SpringBootTest代替@DataJpaTest

相关问题