Java Spring数据测试ExpectedException-未引发异常
我一直在为我的学习项目创建一些测试,但ExpectedException没有按预期工作。我正在做一个没有名字的简单保存(应该抛出一个异常),它没有抛出任何东西 OBS:Java Spring数据测试ExpectedException-未引发异常,java,spring,testing,exception,junit,Java,Spring,Testing,Exception,Junit,我一直在为我的学习项目创建一些测试,但ExpectedException没有按预期工作。我正在做一个没有名字的简单保存(应该抛出一个异常),它没有抛出任何东西 OBS: 当我将SpringBootStarter父级从“2.1.12.RELEASE”更改为 “1.5.4.发布”它是有效的。但我真的想知道为什么 我已经尝试过使用“javax.validation.constraints.NotEmpty”中的NotEmpty 谢谢 ------------Student.java----------
org.hibernate.validator.constraints.NotEmpty
注释现在被弃用(),我认为这是一个原因。尝试改用javax.validation.constraints.NotEmpty
注释
这里已经讨论过这个问题:。使用注释@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());
}
}
@RunWith(SpringRunner.class)
@春靴测试
公共班级学生入学考试{
@自动连线
私立学校;私立学校;
@统治
抛出公共ExpectedException=ExpectedException.none();
私有静态验证器;
@课前
公共静态void SetupValidator实例(){
validator=Validation.buildDefaultValidatorFactory().getValidator();
}
@试验
public void testEmptyNameException(){
expect(TransactionSystemException.class);
this.studentRepository.save(新学生(“,”email@gmail.com"));
}
@试验
public void testemptyname异常消息(){
学生=新学生(“,”email@gmail.com");
设置冲突=validator.validate(学生);
assertEquals(1,违例项.size());
assertEquals(“O campo nome do estudanteéobrigatório”,违例项.iterator().next().getMessage());
}
}
您可以通过使用EntityManager#flush
方法解决此问题-这将强制将托管实体同步到数据库。save
方法不能保证与数据库的同步会立即发生,而且它看起来也不能保证立即进行验证。(我也用1.5版本进行了测试,正如您所注意到的,验证是即时的。)
无论如何,我建议在测试中使用EntityManager#flush
:如果不使用它,代码中也会漏掉一些其他错误(比如用相同的唯一键插入两行)
EntityManager
可以像任何其他bean一样自动连接:
@Autowired
私人实体管理者实体管理者;
中也提到了这种行为 嗨,谢谢你的建议。。但是我已经试过了。。问题还在继续谢谢你,约瑟夫!成功了!你能告诉我在哪里可以找到这些可能破坏我的代码的版本更改吗?经过一段时间的搜索,我真的不确定1.5(Hibernate 5.0)的行为是否真的正确。正如Hibernate常见问题解答所述——调用flush进行验证是预期行为:IMHO最好的方法是你正在做什么——编写测试并尝试找出事情是如何工作的以及如何做得更好。对Spring和Hibernate等主要库进行以下版本更改绝对有帮助,但如果没有测试,您永远无法确定。
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);
}
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());
}
<?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
@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());
}
}