当我运行测试eclipse和控制台时,不同类型的异常?

时间:2014-12-24 20:32:20

标签: 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;

    ...
}

为了测试,我使用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);
}

因此,对于第一个测试userEmailMustBeValid,有一个assertionError,因为它期望一个不会发生的异常,这意味着@Pattern(..)根本不起作用。 对于其余的测试,错误是:

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

HSQL DB和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>

那么,这里有什么问题?

1 个答案:

答案 0 :(得分:1)

断言错误的一个可能原因可能是代码中某处失败的java assert。 Eclipse不会在默认情况下启用断言的情况下运行,而Gradle测试过程会执行。

要确认这是否是问题,您可以尝试在Gradle测试中禁用断言,方法是在build.gradle文件中添加:

test.enableAssertions = false

PS:您可能需要更多地查看堆栈跟踪以找到断言错误的来源。