5 java eclipse spring hibernate hsqldb
我正在使用spring mvc,我在我的域中有Hibernate Validator,我有一些测试在eclipse中传递,但不在控制台中(使用gradle).
在eclipse我只安装了java-7-openjdk-i386,但在我使用的控制台中java version "1.8.0_25",我不知道这是否有用.
我的部分域名是:
@Entity
@Table(name = "users", uniqueConstraints = {
@UniqueConstraint(columnNames = "username"),
@UniqueConstraint(columnNames = "email") })
public class User {
@NotNull
@Pattern(regexp = "(?:[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*|\"(?:[\\x01-\\x08\\x0b\\x0c\\x0e-\\x1f\\x21\\x23-\\x5b\\x5d-\\x7f]|\\\\[\\x01-\\x09\\x0b\\x0c\\x0e-\\x7f])*\")@(?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|\\[(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?|[a-z0-9-]*[a-z0-9]:(?:[\\x01-\\x08\\x0b\\x0c\\x0e-\\x1f\\x21-\\x5a\\x53-\\x7f]|\\\\[\\x01-\\x09\\x0b\\x0c\\x0e-\\x7f])+)\\])")
@Column(name = "email")
private String email;
@NotNull
@Size(min = 6, max = 15)
@Column(name = "username")
private String username;
@NotNull
@Column(name = "role")
private String role;
...
}
Run Code Online (Sandbox Code Playgroud)
为了测试,我使用junit4和嵌入式数据库(HSQL),(对于我使用MySQL的Web应用程序).我的一些测试,抛出异常是:
@Test(expected=javax.validation.ConstraintViolationException.class)
public void userEmailMustBeValid() {
User p = new User("jdoemail.com", "John", "Doe", "johndoe", "johndoe", "USER_ROLE");
userService.create(p);
}
@Test(expected=javax.validation.ConstraintViolationException.class)
public void userEmailCanNotBeNull() {
User p = new User(null, "John", "Doe", "johndoe", "johndoe", "USER_ROLE");
userService.create(p);
}
@Test(expected=javax.validation.ConstraintViolationException.class)
public void userUsernameShouldBeNotNull() {
User p = new User("jroe@mail.com", "John", "Roe", null, "johnroe", "USER_ROLE");
userService.create(p);
}
@Test(expected=javax.validation.ConstraintViolationException.class)
public void userRoleShouldBeNotNull() {
User p = new User("jroe@mail.com", "John", "Roe", "johnroe", "johnroe", null);
userService.create(p);
}
Run Code Online (Sandbox Code Playgroud)
因此,对于第一个测试,userEmailMustBeValid有一个assertionError因为它期望一个不会发生的异常,这意味着@Pattern(..)它根本不起作用.对于其余的测试,错误是:
Unexpected exception, expected<javax.validation.ConstraintViolationException> but was<org.hibernate.exception.ConstraintViolationException>
Run Code Online (Sandbox Code Playgroud)
并且HSQLDB和Hibernate配置如下:
<jdbc:embedded-database id="dataSource" type="HSQL">
<jdbc:script location="classpath:/config/schema.sql"/>
<jdbc:script location="classpath:/config/test-data.sql"/>
</jdbc:embedded-database>
<!-- Hibernate session factory -->
<beans:bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<beans:property name="dataSource" ref="dataSource"/>
<!-- Annotated hibernate clasess -->
<beans:property name="packagesToScan" value="org.munaycoop.taskmanager.domains"/>
<beans:property name="hibernateProperties">
<beans:props>
<beans:prop key="hibernate.cache.provider_class">org.hibernate.cache.internal.NoCacheProvider</beans:prop>
<beans:prop key="hibernate.show_sql">true</beans:prop>
<beans:prop key="hibernate.hbm2ddl.auto">update</beans:prop>
</beans:props>
</beans:property>
</beans:bean>
Run Code Online (Sandbox Code Playgroud)
那么,这里有什么问题?
断言错误的一个可能原因可能是assert代码中某处的 java 失败。默认情况下,Eclipse 不会在启用断言的情况下运行,而 Gradle 测试过程则会这样做。
要确认这是否是问题所在,您可以尝试在 Gradle 测试中禁用断言,方法是在build.gradle文件中添加以下内容:
test.enableAssertions = false
Run Code Online (Sandbox Code Playgroud)
PS:您可能需要更多地查看堆栈跟踪以找到断言错误的根源。