如何在生成的源上进行焊接查找 class
How to make Weld lookup class on generated-sources
我有一个 Maven 项目,我正在使用 MapStruct 生成映射器以帮助将实体转换为 DTO,反之亦然。
此映射器是在 maven 的生成源阶段生成的,并存储在 target/generated-sources 和 target/AppName/WEB-INF/classes 文件夹中。
比如我有这个Mapper
@Mapper
public interface RuleMapper {
RuleDto ruletoDto(Rule rule);
//other cool stuf
}
我将 MapStruct 配置为使用 CDI,因此它将生成以下内容:
@Generated(
value = "org.mapstruct.ap.MappingProcessor",
date = "2016-12-19T23:19:36-0200",
comments = "version: 1.1.0.CR1, compiler: javac, environment: Java 1.8.0_112"
)
@Singleton
@Named
public class RuleMapperImpl implements RuleMapper {
@Override
public RuleDto ruletoDto(Rule rule) {
ruleDto ruleDto = new ruleDto();
if ( rule != null ) {
ruleDto.setIdRule( rule.getIdRule() );
}
return ruleDto;
}
}
它在 Wildfly 服务器上 运行ning 时完美运行,问题是我正在尝试对这个 class 进行 junit 测试,为此,我实现了一个自定义 运行ner如下图:
import org.junit.runners.BlockJUnit4ClassRunner;
import org.junit.runners.model.InitializationError;
public class WeldJUnit4Runner extends BlockJUnit4ClassRunner {
public WeldJUnit4Runner(Class<Object> clazz) throws InitializationError {
super(clazz);
}
@Override
protected Object createTest() throws Exception {
final Class<?> test = getTestClass().getJavaClass();
return WeldContext.INSTANCE.getBean(test);
}
}
并且:
import org.jboss.weld.environment.se.Weld;
import org.jboss.weld.environment.se.WeldContainer;
public class WeldContext {
public static final WeldContext INSTANCE = new WeldContext();
private final Weld weld;
private final WeldContainer container;
private WeldContext() {
this.weld = new Weld();
this.container = weld.initialize();
Runtime.getRuntime().addShutdownHook(new Thread() {
@Override
public void run() {
weld.shutdown();
}
});
}
public <T> T getBean(Class<T> type) {
return container.instance().select(type).get();
}
}
这些实现取自 here。
最后,测试:
@RunWith(WeldJUnit4Runner.class)
public class RuleMapperTest {
@Inject
private RuleMapper ruleMapper;
@Test
public void coolTestName() {
Assert.assertTrue(Boolean.TRUE);
}
}
当我尝试 运行 时,这是控制台输出:
log4j:WARN 找不到记录器 (org.jboss.logging) 的附加程序。
log4j:WARN请正确初始化log4j系统。
log4j:WARN 有关详细信息,请参阅 http://logging.apache.org/log4j/1.2/faq.html#noconfig。
有关日志的警告,以及以下异常:
java.lang.ExceptionInInitializerError
at br.com.treinoos.common.cdi.WeldJUnit4Runner.createTest(WeldJUnit4Runner.java:15)
at org.junit.runners.BlockJUnit4ClassRunner.runReflectiveCall(BlockJUnit4ClassRunner.java:266)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.BlockJUnit4ClassRunner.methodBlock(BlockJUnit4ClassRunner.java:263)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access[=19=]0(ParentRunner.java:58)
at org.junit.runners.ParentRunner.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
Caused by: org.jboss.weld.exceptions.DeploymentException: WELD-001408: Unsatisfied dependencies for type RuleMapper with qualifiers @Default
at injection point [BackedAnnotatedField] @Inject private br.com.treinoos.model.core.business.treinoos.mappers.RuleMapperTest.ruleMapper
at br.com.treinoos.model.core.business.treinoos.mappers.RuleMapperTest.ruleMapper(RuleMapperTest.java:0)
at org.jboss.weld.bootstrap.Validator.validateInjectionPointForDeploymentProblems(Validator.java:359)
at org.jboss.weld.bootstrap.Validator.validateInjectionPoint(Validator.java:281)
at org.jboss.weld.bootstrap.Validator.validateGeneralBean(Validator.java:134)
at org.jboss.weld.bootstrap.Validator.validateRIBean(Validator.java:155)
at org.jboss.weld.bootstrap.Validator.validateBean(Validator.java:518)
at org.jboss.weld.bootstrap.ConcurrentValidator.doWork(ConcurrentValidator.java:68)
at org.jboss.weld.bootstrap.ConcurrentValidator.doWork(ConcurrentValidator.java:66)
at org.jboss.weld.executor.IterativeWorkerTaskFactory.call(IterativeWorkerTaskFactory.java:63)
at org.jboss.weld.executor.IterativeWorkerTaskFactory.call(IterativeWorkerTaskFactory.java:56)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)
Like Weld 无法查找生成的 class。
beans.xml
已在 src/test/resources/META-INF/beans.xml
:
下创建
<?xml version="1.0" encoding="UTF-8"?>
<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"
version="1.1" bean-discovery-mode="all">
</beans>
任何人都可以指出我这个问题的解决方案吗?我已经搜索过类似的东西,但没有成功。
这是对您的问题的完整解释以及我写的内容解决问题的原因。
在 Maven 中,您至少有 2 个类加载器。您的测试类路径和主类路径都有自己的类加载器。根据您的依赖结构,您可以拥有其他人。当 运行 这种方式时,CDI 将每个类加载器识别为单独的 bean 存档。 src/main/webapp
明确用于您的 WAR 文件。 beans.xml
那里没有给你一个bean archive。将 1 添加到 src/main/resources
即可。此问题特定于您如何实例化焊接。
还有其他项目可以正确执行此操作 - CDI-unit and Arquillian,特别是 Weld Embedded 容器。如果您要使用其中之一,这将不是问题。
我有一个 Maven 项目,我正在使用 MapStruct 生成映射器以帮助将实体转换为 DTO,反之亦然。 此映射器是在 maven 的生成源阶段生成的,并存储在 target/generated-sources 和 target/AppName/WEB-INF/classes 文件夹中。
比如我有这个Mapper
@Mapper
public interface RuleMapper {
RuleDto ruletoDto(Rule rule);
//other cool stuf
}
我将 MapStruct 配置为使用 CDI,因此它将生成以下内容:
@Generated(
value = "org.mapstruct.ap.MappingProcessor",
date = "2016-12-19T23:19:36-0200",
comments = "version: 1.1.0.CR1, compiler: javac, environment: Java 1.8.0_112"
)
@Singleton
@Named
public class RuleMapperImpl implements RuleMapper {
@Override
public RuleDto ruletoDto(Rule rule) {
ruleDto ruleDto = new ruleDto();
if ( rule != null ) {
ruleDto.setIdRule( rule.getIdRule() );
}
return ruleDto;
}
}
它在 Wildfly 服务器上 运行ning 时完美运行,问题是我正在尝试对这个 class 进行 junit 测试,为此,我实现了一个自定义 运行ner如下图:
import org.junit.runners.BlockJUnit4ClassRunner;
import org.junit.runners.model.InitializationError;
public class WeldJUnit4Runner extends BlockJUnit4ClassRunner {
public WeldJUnit4Runner(Class<Object> clazz) throws InitializationError {
super(clazz);
}
@Override
protected Object createTest() throws Exception {
final Class<?> test = getTestClass().getJavaClass();
return WeldContext.INSTANCE.getBean(test);
}
}
并且:
import org.jboss.weld.environment.se.Weld;
import org.jboss.weld.environment.se.WeldContainer;
public class WeldContext {
public static final WeldContext INSTANCE = new WeldContext();
private final Weld weld;
private final WeldContainer container;
private WeldContext() {
this.weld = new Weld();
this.container = weld.initialize();
Runtime.getRuntime().addShutdownHook(new Thread() {
@Override
public void run() {
weld.shutdown();
}
});
}
public <T> T getBean(Class<T> type) {
return container.instance().select(type).get();
}
}
这些实现取自 here。
最后,测试:
@RunWith(WeldJUnit4Runner.class)
public class RuleMapperTest {
@Inject
private RuleMapper ruleMapper;
@Test
public void coolTestName() {
Assert.assertTrue(Boolean.TRUE);
}
}
当我尝试 运行 时,这是控制台输出:
log4j:WARN 找不到记录器 (org.jboss.logging) 的附加程序。 log4j:WARN请正确初始化log4j系统。 log4j:WARN 有关详细信息,请参阅 http://logging.apache.org/log4j/1.2/faq.html#noconfig。
有关日志的警告,以及以下异常:
java.lang.ExceptionInInitializerError at br.com.treinoos.common.cdi.WeldJUnit4Runner.createTest(WeldJUnit4Runner.java:15) at org.junit.runners.BlockJUnit4ClassRunner.runReflectiveCall(BlockJUnit4ClassRunner.java:266) at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12) at org.junit.runners.BlockJUnit4ClassRunner.methodBlock(BlockJUnit4ClassRunner.java:263) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57) at org.junit.runners.ParentRunner.run(ParentRunner.java:290) at org.junit.runners.ParentRunner.schedule(ParentRunner.java:71) at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288) at org.junit.runners.ParentRunner.access[=19=]0(ParentRunner.java:58) at org.junit.runners.ParentRunner.evaluate(ParentRunner.java:268) at org.junit.runners.ParentRunner.run(ParentRunner.java:363) at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86) at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192) Caused by: org.jboss.weld.exceptions.DeploymentException: WELD-001408: Unsatisfied dependencies for type RuleMapper with qualifiers @Default at injection point [BackedAnnotatedField] @Inject private br.com.treinoos.model.core.business.treinoos.mappers.RuleMapperTest.ruleMapper at br.com.treinoos.model.core.business.treinoos.mappers.RuleMapperTest.ruleMapper(RuleMapperTest.java:0) at org.jboss.weld.bootstrap.Validator.validateInjectionPointForDeploymentProblems(Validator.java:359) at org.jboss.weld.bootstrap.Validator.validateInjectionPoint(Validator.java:281) at org.jboss.weld.bootstrap.Validator.validateGeneralBean(Validator.java:134) at org.jboss.weld.bootstrap.Validator.validateRIBean(Validator.java:155) at org.jboss.weld.bootstrap.Validator.validateBean(Validator.java:518) at org.jboss.weld.bootstrap.ConcurrentValidator.doWork(ConcurrentValidator.java:68) at org.jboss.weld.bootstrap.ConcurrentValidator.doWork(ConcurrentValidator.java:66) at org.jboss.weld.executor.IterativeWorkerTaskFactory.call(IterativeWorkerTaskFactory.java:63) at org.jboss.weld.executor.IterativeWorkerTaskFactory.call(IterativeWorkerTaskFactory.java:56) at java.util.concurrent.FutureTask.run(FutureTask.java:266) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) at java.lang.Thread.run(Thread.java:745)
Like Weld 无法查找生成的 class。
beans.xml
已在 src/test/resources/META-INF/beans.xml
:
<?xml version="1.0" encoding="UTF-8"?>
<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"
version="1.1" bean-discovery-mode="all">
</beans>
任何人都可以指出我这个问题的解决方案吗?我已经搜索过类似的东西,但没有成功。
这是对您的问题的完整解释以及我写的内容解决问题的原因。
在 Maven 中,您至少有 2 个类加载器。您的测试类路径和主类路径都有自己的类加载器。根据您的依赖结构,您可以拥有其他人。当 运行 这种方式时,CDI 将每个类加载器识别为单独的 bean 存档。 src/main/webapp
明确用于您的 WAR 文件。 beans.xml
那里没有给你一个bean archive。将 1 添加到 src/main/resources
即可。此问题特定于您如何实例化焊接。
还有其他项目可以正确执行此操作 - CDI-unit and Arquillian,特别是 Weld Embedded 容器。如果您要使用其中之一,这将不是问题。