在 Eclipse 和 Maven 中运行基于 SpringBoot 的 Java 单元测试时,我得到了不同的结果。
我的测试成功从命令行使用 maven (mvn test) 运行,但使用 Eclipse 运行失败。
我使用与 JDK 相同的配置,在 Eclipse 中作为 Maven Projekt 导入。所以几乎相同的设置,但没有得到相同的结果:(
有什么建议可以解决这个问题吗?或者有人有类似的问题吗?
其中大部分是我在使用 Eclipse 运行测试时遇到的验证异常,例如:
java.lang.Exception: Unexpected exception, expected<com.MyCustomException> but was<javax.validation.UnexpectedTypeException>
at org.junit.internal.runners.statements.ExpectException.evaluate(ExpectException.java:28)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:75)
at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:86)
或
javax.validation.UnexpectedTypeException: HV000030: No validator could be found for constraint 'javax.validation.constraints.NotEmpty' validating type 'java.lang.String'. Check configuration for 'name'
at org.hibernate.validator.internal.engine.constraintvalidation.ConstraintTree.throwExceptionForNullValidator(ConstraintTree.java:227)
at org.hibernate.validator.internal.engine.constraintvalidation.ConstraintTree.getConstraintValidatorNoUnwrapping(ConstraintTree.java:308)
at org.hibernate.validator.internal.engine.constraintvalidation.ConstraintTree.getConstraintValidatorInstanceForAutomaticUnwrapping ..
但是所有测试都通过了 Maven。
我的测试类标题大多如下所示:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath:context/service-context.xml" })
@Transactional
public class SomeFacadeTest { ...}
谢谢
最佳答案
好的,解决了我的问题...我在 java 构建路径 -> 库选项卡 View 下有一些旧的外部 jar lib 依赖项。我删除了这些并且测试成功运行:)
关于java - 使用 Eclipse 和 Maven 运行 SpringBoot JUnit-Tests 时的不同结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53775728/