标题翻译
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 = "primary")
	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("primary").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.
<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>
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("testunitname").createEntityManager();
    }
}
Then define your test alternative class on test beans.xml as described on your question.
专注分享java语言的经验与见解,让所有开发者获益!



评论