在单元测试中通过CDI检索替代的entityManager。

huangapple 未分类评论58阅读模式
标题翻译

Retrieve Alternative entityManager on unit test though CDI

问题

我正在使用Junit 5,在webapp EE8环境下运行的Java批处理。

在Web应用程序中,实际上我有一个被用作生产者的资源类:

@ApplicationScoped
public class Resources {

    @Produces
    public Logger produceLog(InjectionPoint injectionPoint) {
        return LoggerFactory.getLogger(injectionPoint.getMember().getDeclaringClass().getName());
    }

    @Produces
    @PersistenceContext(unitName = "primary")
    private EntityManager entityManager;

}

现在我想编写一些SE(标准版)测试,并且需要获取一个备用的实体管理器,类似于:

public class MockResources {

    @Alternative
    @JobScoped
    @Produces
    public EntityManager getEntityManager() {
        return Persistence.createEntityManagerFactory("primary").createEntityManager();
    }

}

问题是我不知道如何检索这个备用的实体管理器,因为 beans.xml 需要一个类(我尝试过使用Hibernate SessionImpl 但它不起作用),也不适合使用 @Stereotype。

<beans xmlns="http://xmlns.jcp.org/xml/ns/javaee"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/beans_1_1.xsd"
       bean-discovery-mode="all">
    <alternatives>
        <class>org.hibernate.internal.SessionImpl</class>
    </alternatives>
</beans>

有任何帮助吗?

英文翻译

I'm using Junit 5, Java bath that run under a webapp EE8 environment.

On the web app, I actually have a resources class that is employed as a producer:

@ApplicationScoped
public class Resources {

	@Produces
	public Logger produceLog(InjectionPoint injectionPoint) {
		return LoggerFactory.getLogger(injectionPoint.getMember().getDeclaringClass().getName());
	}

	@Produces
	@PersistenceContext(unitName = &quot;primary&quot;)
	private EntityManager entityManager;

}

Now I want to write some SE test, and I need to retrieve an alternative entity manager, something like:

public class MockResources {

	@Alternative
	@JobScoped
	@Produces
	public EntityManager getEntityManager() {
		return Persistence.createEntityManagerFactory(&quot;primary&quot;).createEntityManager();
	}

}

The issue is that I don't know how to retrieve this alternative entity , as beans.xml want a class (I tried with Hibernate SessionImpl but it doesn't work), neither @Stereotype looks good for my case.

&lt;beans xmlns=&quot;http://xmlns.jcp.org/xml/ns/javaee&quot;
	   xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;
	   xsi:schemaLocation=&quot;http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/beans_1_1.xsd&quot;
	   bean-discovery-mode=&quot;all&quot;&gt;
	&lt;alternatives&gt;
		&lt;class&gt;org.hibernate.internal.SessionImpl&lt;/class&gt;
	&lt;/alternatives&gt;
&lt;/beans&gt;

Any help ?

答案1

得分: 0

你应该创建一个完整的@Alternative资源生产者bean,就像这样:

@Alternative
@ApplicationScoped
public class TestResources {
    @Produces
    public Logger produceLog(InjectionPoint injectionPoint) {
        return LoggerFactory.getLogger(injectionPoint.getMember().getDeclaringClass().getName());
    }

    @Produces
    private EntityManager getEntityManager() {
        // 在此处创建新的EntityManager
        return Persistence.createEntityManagerFactory("testunitname").createEntityManager();
    }
}

然后按照你的问题中描述的方式,在测试的beans.xml文件中定义你的测试备选类。

英文翻译

You should create an entire @Alternative Resources producer bean, as in

@Alternative
@ApplicationScoped
public class TestResources {
    @Produces
    public Logger produceLog(InjectionPoint injectionPoint) {
        return LoggerFactory.getLogger(injectionPoint.getMember().getDeclaringClass().getName());
    }

    @Produces
    private EntityManager getEntityManager() {
        // create your new entitymanager here
        return Persistence.createEntityManagerFactory(&quot;testunitname&quot;).createEntityManager();
    }
}

Then define your test alternative class on test beans.xml as described on your question.

huangapple
  • 本文由 发表于 2020年1月30日 18:38:17
  • 转载请务必保留本文链接:https://java.coder-hub.com/59984064.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定