Different kind of exception when I run the test both eclipse and console?

I am using spring mvc and I have Hibernate Validator in my domains and I have a test that passes in eclipse but not in the console (using gradle).

In eclipse I 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 to do with.

Part of my domain:

 @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 the built-in database (HSQL) (for a web application I use MySQL). Some of my tests that 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 assertionError because it expects an exception that does not occur, which means that @Pattern(..) doesn't work at all. And for the rest of the tests, the error is:

 Unexpected exception, expected<javax.validation.ConstraintViolationException> but was<org.hibernate.exception.ConstraintViolationException> 

Both HSQL DB and Hibernate are configured as follows:

 <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?

+5
source share
1 answer

One of the possible causes of an assertion error may be a java assert error somewhere in the code. Eclipse does not start with statements enabled by default, while the Gradle testing process is running.

To make sure this is a problem, you can try disabling statements in Gradle tests by adding this to your build.gradle file:

 test.enableAssertions = false 

PS: You might want to look into stacktrace a bit more to find the source of the assertion error.

+1
source

Source: https://habr.com/ru/post/1209856/


All Articles