I'm using spring mvc and i have Hibernate Validator in my domains, and i have some test that pass in eclipse, but doesn't in console (using gradle).
In eclipse i have installed only java-7-openjdk-i386
, but in the console i use java version "1.8.0_25"
, i don't know if this has something with.
Part of my domain is:
@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;
...
}
For testing, i use junit4 and an embedded database(HSQL), (for the web application i use MySQL). Some of my test, which throw exceptions are:
@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);
}
So, for the first test userEmailMustBeValid
there is an assertionError
because it expects an exception that doesn't occur, which means the @Pattern(..)
is not working at all.
And for the rest of the tests the error is:
Unexpected exception, expected<javax.validation.ConstraintViolationException> but was<org.hibernate.exception.ConstraintViolationException>
And the HSQL
DB and Hibernate are configured like this:
<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>
So, what is wrong here?
One possible reason for the assertion error might be a failing java assert
somewhere in the code. Eclipse doesn't run with assertions enabled by default, whereas the Gradle test process does.
To confirm if this is the problem, you can try disabling assertions in Gradle tests, by adding this in your build.gradle
file:
test.enableAssertions = false
PS: You might want to look into the stacktrace a bit more to find the source of the assertion error.