Spring - 来自 JUnit 和 Maven 的 运行 时不同的测试结果
Spring - Different test results when running from JUnit and Maven
我遇到了一个不知道如何解决的问题。我正在开发一个 Spring MVC 应用程序及其单元和集成测试。一切在 JUnit 下运行良好(运行 As -> JUnit 测试)但是当我在 Maven 下失败 =59=] (Surefire 2.10) 我有两种类型的错误:
模型测试
testFileNameCannotBeNull(es.kazbeel.geckobugtracker.model.AttachmentTest): Unexpected exception, expected<org.hibernate.exception.ConstraintViolationException> but was<org.hibernate.PropertyValueException>
控制器测试
testCreateEnvironmentPost_withBindingResultErrors(es.kazbeel.geckobugtracker.web.controller.admin.EnvironmentControllerTest): View name expected:</admin/environments/CreateEnvironment> but was:<redirect:/admin/environments/ViewEnvironments>
AttachmentTest(仅失败的方法)
@Test(expected = ConstraintViolationException.class)
@DatabaseSetup("AttachmentTest.xml")
@Transactional
public void testFileNameCannotBeNull () {
Session session = sessionFactory.getCurrentSession();
Issue issue = (Issue) session.get(Issue.class, 1);
User author = (User) session.get(User.class, 1);
Attachment attch = AttachmentBuilder.attachment()
.withIssue(issue)
.withAuthor(author)
.withFileName(null)
.withFileSize(new Long(0))
.withFileMimeType("file_mimetype")
.build();
session.save(attch);
session.flush();
session.clear();
}
在这种情况下,模型配置在hbm.xml中。 属性如下
<property generated="never" lazy="false" length="255" name="fileName" column="FILE_NAME" not-null="true" type="java.lang.String">
EnvironmentControllerTest(仅失败的方法)
@Test
public void testCreateEnvironmentPost_withBindingResultErrors () throws Exception {
mockMvc.perform(post("/admin/environments/CreateEnvironment").param("name", ""))
.andExpect(view().name("/admin/environments/CreateEnvironment"))
.andExpect(model().hasErrors())
.andExpect(model().size(1))
.andExpect(model().attributeExists("environment"));
verify(issueService, never()).saveEnvironment(any(Environment.class));
}
EnvironmentController(仅方法)
@RequestMapping(value = "/CreateEnvironment", method = RequestMethod.POST)
public String createEnvironmentPost (@Valid @ModelAttribute("environment") Environment environment,
BindingResult result) {
LOG.debug("Creating new Environment");
if (result.hasErrors() == true) {
LOG.debug("The environment is not valid: {}", result.getFieldError().getDefaultMessage());
return CREATE_URL;
}
try {
issueService.saveEnvironment(environment);
} catch (DataIntegrityViolationException ex) {
LOG.debug(ex.getMessage());
return CREATE_URL;
}
return "redirect:" + VIEW_URL;
}
pom.xml(只与surefire有关)
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.10</version>
<executions>
<execution>
<id>default-test</id>
<phase>test</phase>
<goals>
<goal>test</goal>
</goals>
</execution>
</executions>
</plugin>
真诚地,我迷路了(关于这个问题,我什至不知道我应该向您展示什么样的信息)。
有人知道会发生什么吗?网上没搜到类似的冲浪。
!更新!
删除 pom.xml 中的依赖项(见下文)(但保留 javax.validation
)我得到相同的异常 运行 JUnit 下的测试。
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>5.1.1.Final</version>
</dependency>
好的!这是向前迈出的一步,但这让我想知道 this assertion 是否正确。我确信验证是第一个 verification/check 但似乎 Hibernate 的属性检查首先接管。我说得对吗?
!已解决!
第一
问题。在同一个项目中包含两个验证器时,似乎是一种不兼容:
<dependency>
<groupId>javax.validation</groupId>
<artifactId>validation-api</artifactId>
<version>1.1.0.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>5.1.1.Final</version>
</dependency>
解决方案。我从 pom.xml.
中删除了 javax.validation
注意:如果我错了,我将不胜感激。
第二
问题。解决了第一个问题后我仍然没有通过测试 运行 成功。
解决方案。我在Hibernate Validatorwebpage官方
上找到了解决方案
Unified Expression Language (EL) Hibernate Validator also requires an
implementation of the Unified Expression Language (JSR 341) for
evaluating dynamic expressions in constraint violation messages. When
your application runs in a Java EE container such as WildFly, an EL
implementation is already provided by the container. In a Java SE
environment, however, you have to add an implementation as dependency
to your POM file. For instance you can add the following two
dependencies to use the JSR 341 reference implementation:
<dependency>
<groupId>javax.el</groupId>
<artifactId>javax.el-api</artifactId>
<version>2.2.4</version>
</dependency>
<dependency>
<groupId>org.glassfish.web</groupId>
<artifactId>javax.el</artifactId>
<version>2.2.4</version>
</dependency>
我遇到了一个不知道如何解决的问题。我正在开发一个 Spring MVC 应用程序及其单元和集成测试。一切在 JUnit 下运行良好(运行 As -> JUnit 测试)但是当我在 Maven 下失败 =59=] (Surefire 2.10) 我有两种类型的错误:
模型测试
testFileNameCannotBeNull(es.kazbeel.geckobugtracker.model.AttachmentTest): Unexpected exception, expected<org.hibernate.exception.ConstraintViolationException> but was<org.hibernate.PropertyValueException>
控制器测试
testCreateEnvironmentPost_withBindingResultErrors(es.kazbeel.geckobugtracker.web.controller.admin.EnvironmentControllerTest): View name expected:</admin/environments/CreateEnvironment> but was:<redirect:/admin/environments/ViewEnvironments>
AttachmentTest(仅失败的方法)
@Test(expected = ConstraintViolationException.class)
@DatabaseSetup("AttachmentTest.xml")
@Transactional
public void testFileNameCannotBeNull () {
Session session = sessionFactory.getCurrentSession();
Issue issue = (Issue) session.get(Issue.class, 1);
User author = (User) session.get(User.class, 1);
Attachment attch = AttachmentBuilder.attachment()
.withIssue(issue)
.withAuthor(author)
.withFileName(null)
.withFileSize(new Long(0))
.withFileMimeType("file_mimetype")
.build();
session.save(attch);
session.flush();
session.clear();
}
在这种情况下,模型配置在hbm.xml中。 属性如下
<property generated="never" lazy="false" length="255" name="fileName" column="FILE_NAME" not-null="true" type="java.lang.String">
EnvironmentControllerTest(仅失败的方法)
@Test
public void testCreateEnvironmentPost_withBindingResultErrors () throws Exception {
mockMvc.perform(post("/admin/environments/CreateEnvironment").param("name", ""))
.andExpect(view().name("/admin/environments/CreateEnvironment"))
.andExpect(model().hasErrors())
.andExpect(model().size(1))
.andExpect(model().attributeExists("environment"));
verify(issueService, never()).saveEnvironment(any(Environment.class));
}
EnvironmentController(仅方法)
@RequestMapping(value = "/CreateEnvironment", method = RequestMethod.POST)
public String createEnvironmentPost (@Valid @ModelAttribute("environment") Environment environment,
BindingResult result) {
LOG.debug("Creating new Environment");
if (result.hasErrors() == true) {
LOG.debug("The environment is not valid: {}", result.getFieldError().getDefaultMessage());
return CREATE_URL;
}
try {
issueService.saveEnvironment(environment);
} catch (DataIntegrityViolationException ex) {
LOG.debug(ex.getMessage());
return CREATE_URL;
}
return "redirect:" + VIEW_URL;
}
pom.xml(只与surefire有关)
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.10</version>
<executions>
<execution>
<id>default-test</id>
<phase>test</phase>
<goals>
<goal>test</goal>
</goals>
</execution>
</executions>
</plugin>
真诚地,我迷路了(关于这个问题,我什至不知道我应该向您展示什么样的信息)。
有人知道会发生什么吗?网上没搜到类似的冲浪。
!更新!
删除 pom.xml 中的依赖项(见下文)(但保留 javax.validation
)我得到相同的异常 运行 JUnit 下的测试。
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>5.1.1.Final</version>
</dependency>
好的!这是向前迈出的一步,但这让我想知道 this assertion 是否正确。我确信验证是第一个 verification/check 但似乎 Hibernate 的属性检查首先接管。我说得对吗?
!已解决!
第一
问题。在同一个项目中包含两个验证器时,似乎是一种不兼容:
<dependency>
<groupId>javax.validation</groupId>
<artifactId>validation-api</artifactId>
<version>1.1.0.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>5.1.1.Final</version>
</dependency>
解决方案。我从 pom.xml.
中删除了javax.validation
注意:如果我错了,我将不胜感激。
第二
问题。解决了第一个问题后我仍然没有通过测试 运行 成功。
解决方案。我在Hibernate Validatorwebpage官方
上找到了解决方案Unified Expression Language (EL) Hibernate Validator also requires an implementation of the Unified Expression Language (JSR 341) for evaluating dynamic expressions in constraint violation messages. When your application runs in a Java EE container such as WildFly, an EL implementation is already provided by the container. In a Java SE environment, however, you have to add an implementation as dependency to your POM file. For instance you can add the following two dependencies to use the JSR 341 reference implementation:
<dependency>
<groupId>javax.el</groupId>
<artifactId>javax.el-api</artifactId>
<version>2.2.4</version>
</dependency>
<dependency>
<groupId>org.glassfish.web</groupId>
<artifactId>javax.el</artifactId>
<version>2.2.4</version>
</dependency>