相关疑难解决方法(0)

TestNG和Spring 3

使用Spring进行单元测试的最佳实践方法是什么?我假设SpringN的组合TestNG和jmockit并不坏,所以这就是我现在正在做的事情,但如果我当然没有为我的新Spring项目选择工具,请马上告诉我.:-)

无论如何,我已经创建了一个我想测试的实体,但我不确定如何从Spring上下文中实际运行TestNG.我创建了一个简单的测试类

package com.mydomain.data.entities.test;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.testng.AbstractTestNGSpringContextTests;
import org.testng.Assert;
import org.testng.annotations.*;

@ContextConfiguration(locations={"classpath:applicationContext.xml"}) 
public class SimpleTest extends AbstractTestNGSpringContextTests {

    @Autowired
    private ApplicationContext applicationContext;

    @BeforeClass
    protected void setUp() throws Exception {
        Assert.assertNotNull(applicationContext);
    }

    @Test
    public void testNothing() {

    }

}
Run Code Online (Sandbox Code Playgroud)

使用applicationContext.xml导入bean以设置模型和业务层.首先,我希望它使用Project/WebRoot/WEB-INF中的applicationContext.xml,而此测试的源代码位于Project/src/com/mydomain/data/entities/test中,但我相信/applicationContext.xml会给我Projects/src,和classpath一样:这是正确的吗?

此外,既然我正在使用我的Web应用程序将使用的模型和业务层,我应该期望它的行为类似.但是,当我在测试中启动TestNG时,它是sais:class org.hibernate.cfg.ExtendedMappings将接口org.hibernate.cfg.Mappings作为超类:

Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in URL [file:/Users/niklas/Documents/Eclipse/Project/WebRoot/WEB-INF/Project-Model.xml]: Invocation of init method failed; nested exception is java.lang.IncompatibleClassChangeError: class org.hibernate.cfg.ExtendedMappings has interface org.hibernate.cfg.Mappings as super class
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1393)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:511) …
Run Code Online (Sandbox Code Playgroud)

java testng spring unit-testing beancreationexception

6
推荐指数
2
解决办法
2万
查看次数

作为实体的密封类

我对密封课程有疑问。如果我从 docker 运行我的应用程序,它工作得很好,但如果我在 IntelliJ 中执行相同的操作,我会遇到以下异常:

java.lang.IncompatibleClassChangeError: class com.nemethlegtechnika.products.model.Attribute$HibernateProxy$D0WxdNVz cannot inherit from sealed class com.nemethlegtechnika.products.model.Attribute

如果我使用抽象类而不是密封类,那么在 IntelliJ 和 Docker 中都不会出现错误。你们能帮我找到问题的根源吗?

提前致谢,祝您有美好的一天!:)

课程:
package com.nemethlegtechnika.products.model

import jakarta.persistence.*
import org.hibernate.annotations.DiscriminatorFormula

@Entity
@Table(name = "attribute")
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorFormula("case when stringValues is not null then 'string' else 'boolean' end")
sealed class Attribute : BaseEntity() {

    @ManyToOne(fetch = FetchType.EAGER, optional = false)
    @JoinColumn(name = "product_id")
    val product: Product? = null

    @ManyToOne(fetch = FetchType.EAGER, optional = false)
    @JoinColumn(name = "group_id")
    val group: Group? = null

    abstract …
Run Code Online (Sandbox Code Playgroud)

spring hibernate kotlin docker sealed-class

5
推荐指数
1
解决办法
993
查看次数