【问题标题】:Spring Data Test ExpectedException - No exception thrownSpring Data Test ExpectedException - 没有抛出异常
【发布时间】:2020-06-08 10:47:25
【问题描述】:

我一直在为我的学习项目创建一些测试,但 ExpectedException 没有按预期工作。我正在做一个没有名称的简单保存(应该抛出异常)并且它没有抛出任何东西。

OBS:

  1. 当我将 spring-boot-starter-parent 从“2.1.12.RELEASE”更改为 “1.5.4.RELEASE”它有效。但我真的很想知道为什么。

  2. 我已经尝试过使用“javax.validation.constraints.NotEmpty”中的 NotEmpty

谢谢!

------------ Student.java ------------

import org.hibernate.validator.constraints.Email;
import org.hibernate.validator.constraints.Length;
import org.hibernate.validator.constraints.NotEmpty;

@Entity
public class Student extends AbstractEntity{

public Student() {
}

public Student(String name, String email) {
    this.name = name;
    this.email = email;
}

@NotEmpty(message = "O campo nome do estudante é obrigatório")
private String name;

@NotEmpty
@Email
//    @Column(columnDefinition = "varchar(255) default 'John Snow'")
    private String email;

------------ StudentRepository.java ------------

import br.com.devdojo.awesome.model.Student;
import org.springframework.data.repository.CrudRepository;
import org.springframework.data.repository.PagingAndSortingRepository;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Repository;

import java.util.List;

public interface StudentRepository extends PagingAndSortingRepository<Student, Long> {
    List<Student> findByNameIgnoreCaseContaining(String name);
}

------------ StudentRepositoryTest.java ------------

import static org.assertj.core.api.Assertions.*;

@RunWith(SpringRunner.class)
@DataJpaTest
public class StudentRepositoryTest {

    @Autowired
    private StudentRepository studentRepository;

    @Rule
    public ExpectedException thrown = ExpectedException.none();

    @Test
    public void createWhenNameIsNullShouldThrownConstraintViolationException() {
        thrown.expect(ConstraintViolationException.class);
        thrown.expectMessage("O campo nome do estudante é obrigatório");
        this.studentRepository.save(new Student());
    }

------------ 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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>groupId</groupId>
<artifactId>Projects</artifactId>
<version>1.0-SNAPSHOT</version>
<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <configuration>
                <source>8</source>
                <target>8</target>
            </configuration>
        </plugin>
    </plugins>
</build>
<packaging>jar</packaging>

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.1.12.RELEASE</version>
</parent>




<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-actuator</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-devtools</artifactId>
        <optional>true</optional>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-security</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.security</groupId>
        <artifactId>spring-security-test</artifactId>
        <version>4.2.3.RELEASE</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>com.oracle</groupId>
        <artifactId>ojdbc8</artifactId>
        <version>1.0</version>
        <scope>system</scope>
        <systemPath>C:/sqldeveloper/jdbc/lib/ojdbc8.jar</systemPath>
    </dependency>


    <!-- Use MySQL Connector-J -->
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <scope>runtime</scope>
        <version>8.0.13</version>
    </dependency>

    <dependency>
        <groupId>commons-io</groupId>
        <artifactId>commons-io</artifactId>
        <version>2.5</version>
    </dependency>

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

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <version>1.5.4.RELEASE</version>
        <scope>test</scope>
    </dependency>

</dependencies>
</project>

------------ 错误信息 ------------

2020-02-24 15:16:01.788  INFO 16196 --- [           main] org.hibernate.Version                    : HHH000412: Hibernate Core {5.3.15.Final}
2020-02-24 15:16:01.790  INFO 16196 --- [           main] org.hibernate.cfg.Environment            : HHH000206: hibernate.properties not found
2020-02-24 15:16:01.997  INFO 16196 --- [           main] o.hibernate.annotations.common.Version   : HCANN000001: Hibernate Commons Annotations {5.0.4.Final}
2020-02-24 15:16:02.216  INFO 16196 --- [           main] org.hibernate.dialect.Dialect            : HHH000400: Using dialect: org.hibernate.dialect.H2Dialect
Hibernate: create table spring_user (id bigint not null, admin boolean not null, name varchar(255) not null, password varchar(255) not null, user_name varchar(255) not null, primary key (id))
Hibernate: create table student (id bigint not null, email varchar(255), name varchar(255), primary key (id))
Hibernate: alter table spring_user drop constraint if exists UK_6p8pvty87x0ovxe0v5ght3l3r
Hibernate: alter table spring_user add constraint UK_6p8pvty87x0ovxe0v5ght3l3r unique (user_name)
Hibernate: create sequence hibernate_sequence start with 1 increment by 1
2020-02-24 15:16:03.166  INFO 16196 --- [           main] j.LocalContainerEntityManagerFactoryBean : Initialized JPA EntityManagerFactory for persistence unit 'default'
2020-02-24 15:16:03.721  INFO 16196 --- [           main] b.c.d.awesome.StudentRepositoryTest      : Started StudentRepositoryTest in 5.05 seconds (JVM running for 6.214)
2020-02-24 15:16:03.745  INFO 16196 --- [           main] o.s.t.c.transaction.TransactionContext   : Began transaction (1) for test context [DefaultTestContext@25ce9dc4 testClass = StudentRepositoryTest, testInstance = br.com.devdojo.awesome.StudentRepositoryTest@742ff096, testMethod = createWhenEmailIsNullShouldThrownConstraintViolationException@StudentRepositoryTest, testException = [null], mergedContextConfiguration = [MergedContextConfiguration@74ea2410 testClass = StudentRepositoryTest, locations = '{}', classes = '{class br.com.devdojo.awesome.ApplicationStart}', contextInitializerClasses = '[]', activeProfiles = '{}', propertySourceLocations = '{}', propertySourceProperties = '{org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTestContextBootstrapper=true}', contextCustomizers = set[[ImportsContextCustomizer@17f62e33 key = [org.springframework.boot.autoconfigure.cache.CacheAutoConfiguration, org.springframework.boot.autoconfigure.data.jpa.JpaRepositoriesAutoConfiguration, org.springframework.boot.autoconfigure.flyway.FlywayAutoConfiguration, org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration, org.springframework.boot.autoconfigure.jdbc.DataSourceTransactionManagerAutoConfiguration, org.springframework.boot.autoconfigure.jdbc.JdbcTemplateAutoConfiguration, org.springframework.boot.autoconfigure.liquibase.LiquibaseAutoConfiguration, org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration, org.springframework.boot.autoconfigure.transaction.TransactionAutoConfiguration, org.springframework.boot.test.autoconfigure.jdbc.TestDatabaseAutoConfiguration, org.springframework.boot.test.autoconfigure.orm.jpa.TestEntityManagerAutoConfiguration]], org.springframework.boot.test.context.filter.ExcludeFilterContextCustomizer@3eb738bb, org.springframework.boot.test.json.DuplicateJsonObjectContextCustomizerFactory$DuplicateJsonObjectContextCustomizer@185a6e9, org.springframework.boot.test.mock.mockito.MockitoContextCustomizer@0, org.springframework.boot.test.autoconfigure.OverrideAutoConfigurationContextCustomizerFactory$DisableAutoConfigurationContextCustomizer@49438269, org.springframework.boot.test.autoconfigure.filter.TypeExcludeFiltersContextCustomizer@351584c0, org.springframework.boot.test.autoconfigure.properties.PropertyMappingContextCustomizer@36807d48, org.springframework.boot.test.autoconfigure.web.servlet.WebDriverContextCustomizerFactory$Customizer@6f4a47c7], contextLoader = 'org.springframework.boot.test.context.SpringBootContextLoader', parent = [null]], attributes = map[[empty]]]; transaction manager [org.springframework.orm.jpa.JpaTransactionManager@4e85dcb2]; rollback [true]
Hibernate: call next value for hibernate_sequence
Hibernate: call next value for hibernate_sequence
Disconnected from the target VM, address: '127.0.0.1:55458', transport: 'socket'
2020-02-24 15:16:38.447  INFO 16196 --- [           main] o.s.t.c.transaction.TransactionContext   : Rolled back transaction for test: [DefaultTestContext@25ce9dc4 testClass = StudentRepositoryTest, testInstance = br.com.devdojo.awesome.StudentRepositoryTest@742ff096, testMethod = createWhenEmailIsNullShouldThrownConstraintViolationException@StudentRepositoryTest, testException = [null], mergedContextConfiguration = [MergedContextConfiguration@74ea2410 testClass = StudentRepositoryTest, locations = '{}', classes = '{class br.com.devdojo.awesome.ApplicationStart}', contextInitializerClasses = '[]', activeProfiles = '{}', propertySourceLocations = '{}', propertySourceProperties = '{org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTestContextBootstrapper=true}', contextCustomizers = set[[ImportsContextCustomizer@17f62e33 key = [org.springframework.boot.autoconfigure.cache.CacheAutoConfiguration, org.springframework.boot.autoconfigure.data.jpa.JpaRepositoriesAutoConfiguration, org.springframework.boot.autoconfigure.flyway.FlywayAutoConfiguration, org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration, org.springframework.boot.autoconfigure.jdbc.DataSourceTransactionManagerAutoConfiguration, org.springframework.boot.autoconfigure.jdbc.JdbcTemplateAutoConfiguration, org.springframework.boot.autoconfigure.liquibase.LiquibaseAutoConfiguration, org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration, org.springframework.boot.autoconfigure.transaction.TransactionAutoConfiguration, org.springframework.boot.test.autoconfigure.jdbc.TestDatabaseAutoConfiguration, org.springframework.boot.test.autoconfigure.orm.jpa.TestEntityManagerAutoConfiguration]], org.springframework.boot.test.context.filter.ExcludeFilterContextCustomizer@3eb738bb, org.springframework.boot.test.json.DuplicateJsonObjectContextCustomizerFactory$DuplicateJsonObjectContextCustomizer@185a6e9, org.springframework.boot.test.mock.mockito.MockitoContextCustomizer@0, org.springframework.boot.test.autoconfigure.OverrideAutoConfigurationContextCustomizerFactory$DisableAutoConfigurationContextCustomizer@49438269, org.springframework.boot.test.autoconfigure.filter.TypeExcludeFiltersContextCustomizer@351584c0, org.springframework.boot.test.autoconfigure.properties.PropertyMappingContextCustomizer@36807d48, org.springframework.boot.test.autoconfigure.web.servlet.WebDriverContextCustomizerFactory$Customizer@6f4a47c7], contextLoader = 'org.springframework.boot.test.context.SpringBootContextLoader', parent = [null]], attributes = map[[empty]]]

java.lang.AssertionError: Expected test to throw (an instance of java.lang.IllegalStateException and exception with message a string containing "O campo nome do estudante é obrigatório")

at org.junit.Assert.fail(Assert.java:88)
at org.junit.rules.ExpectedException.failDueToMissingException(ExpectedException.java:263)
at org.junit.rules.ExpectedException.access$200(ExpectedException.java:106)
at org.junit.rules.ExpectedException$ExpectedExceptionStatement.evaluate(ExpectedException.java:245)
at org.junit.rules.RunRules.evaluate(RunRules.java:20)
at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:84)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:251)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:97)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:190)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
at com.intellij.rt.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:33)
at com.intellij.rt.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:230)
at com.intellij.rt.junit.JUnitStarter.main(JUnitStarter.java:58)

2020-02-24 15:16:38.461  INFO 16196 --- [       Thread-3] j.LocalContainerEntityManagerFactoryBean : Closing JPA EntityManagerFactory for persistence unit 'default'

Process finished with exit code -1

【问题讨论】:

    标签: java spring testing exception junit


    【解决方案1】:

    您可以使用EntityManager#flush 方法解决此问题 - 这会强制将您的托管实体同步到数据库。 save 方法不保证与数据库的同步会立即发生,而且看起来它也不保证验证会立即完成。 (我也用 1.5 版本测试过,正如你所注意到的,验证是即时的。)

    无论如何,我建议在您的测试中使用EntityManager#flush:如果没有它,您还可能会错过代码中的其他一些错误(例如插入具有相同唯一键的两行)。

    EntityManager 可以像任何其他 bean 一样自动装配:

    @Autowired
    private EntityManager entityManager;
    

    Hibernate's FAQ 中也提到了这种行为。

    【讨论】:

    • 谢谢你,约瑟夫!有效!你能告诉我在哪里可以找到这些可能破坏我的代码的版本更改吗?
    • 经过一番搜索后,我不确定 1.5(Hibernate 5.0)的行为是否真的正确。正如 Hibernate 常见问题解答所述 - 调用刷新进行验证是预期行为:hibernate.org/validator/faq/… 恕我直言,最好的方法是你正在做的事情 - 编写测试并尝试找出事情是如何工作的以及如何做得更好。跟踪 Spring 和 Hibernate 等主要库的版本更改绝对有帮助,但如果没有测试,您永远无法确定。
    【解决方案2】:

    使用注解@SpringBootTest 而不是@DataJpaTest,以加载完整的Spring 上下文。要测试约束消息,您应该使用 Validator.validate()。 将您的测试类更改如下:

    @RunWith(SpringRunner.class)
    @SpringBootTest
    public class StudentRepositoryTest {
    
    @Autowired
    private StudentRepository studentRepository;
    
    @Rule
    public ExpectedException thrown = ExpectedException.none();
    
    private static Validator validator;
    
    @BeforeClass
    public static void setupValidatorInstance() {
        validator = Validation.buildDefaultValidatorFactory().getValidator();
    }
    
    @Test
    public void testEmptyNameException() {
        thrown.expect(TransactionSystemException.class);
    
        this.studentRepository.save(new Student("","email@gmail.com"));
    }
    
    @Test
    public void testEmptyNameExceptionMessage() {
        Student student = new Student("","email@gmail.com");
    
        Set<ConstraintViolation<Student>> violations = validator.validate(student);
    
        assertEquals(1 , violations.size());
        assertEquals("O campo nome do estudante é obrigatório", violations.iterator().next().getMessage());
    }
    }
    

    【讨论】:

      【解决方案3】:

      org.hibernate.validator.constraints.NotEmpty 注释现在已弃用(annotation type NotEmpty),我认为这是一个原因。尝试改用javax.validation.constraints.NotEmpty 注解。

      这里已经讨论过这个问题:Hibernate @NotEmpty is deprecated

      【讨论】:

      • 您好,感谢您的建议。但我已经尝试过了。问题还在继续
      猜你喜欢
      • 2015-10-03
      • 1970-01-01
      • 2018-03-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-02-22
      • 1970-01-01
      相关资源
      最近更新 更多