如何在生成源

时间:2016-12-20 22:45:06

标签: maven java-ee cdi weld mapstruct

我有一个maven项目,我正在使用MapStruct来生成映射器,以帮助将实体转换为DTO,反之亦然。 此映射器在maven的generate-sources阶段生成,并存储到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服务器上运行时,它完全可行,问题是我正在尝试对这个类进行junit测试,为此,我实现了一个自定义运行器,如下所示:

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找不到logger(org.jboss.logging)的appender。 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)       在org.junit.runners.BlockJUnit4ClassRunner $ 1.runReflectiveCall(BlockJUnit4ClassRunner.java:266)       在org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)       在org.junit.runners.BlockJUnit4ClassRunner.methodBlock(BlockJUnit4ClassRunner.java:263)       在org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)       在org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)       在org.junit.runners.ParentRunner $ 3.run(ParentRunner.java:290)       在org.junit.runners.ParentRunner $ 1.schedule(ParentRunner.java:71)       在org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)       在org.junit.runners.ParentRunner.access $ 000(ParentRunner.java:58)       在org.junit.runners.ParentRunner $ 2.evaluate(ParentRunner.java:268)       在org.junit.runners.ParentRunner.run(ParentRunner.java:363)       在org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)       在org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)       在org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)       在org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)       在org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)       在org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)   引起:org.jboss.weld.exceptions.DeploymentException:WELD-001408:类型为RuleMapper且带有限定符@Default的依赖项不满意     在注入点[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)       在org.jboss.weld.bootstrap.Validator.validateInjectionPointForDeploymentProblems(Validator.java:359)       在org.jboss.weld.bootstrap.Validator.validateInjectionPoint(Validator.java:281)       在org.jboss.weld.bootstrap.Validator.validateGeneralBean(Validator.java:134)       在org.jboss.weld.bootstrap.Validator.validateRIBean(Validator.java:155)       在org.jboss.weld.bootstrap.Validator.validateBean(Validator.java:518)       在org.jboss.weld.bootstrap.ConcurrentValidator $ 1.doWork(ConcurrentValidator.java:68)       在org.jboss.weld.bootstrap.ConcurrentValidator $ 1.doWork(ConcurrentValidator.java:66)       在org.jboss.weld.executor.IterativeWorkerTaskFactory $ 1.call(IterativeWorkerTaskFactory.java:63)       在org.jboss.weld.executor.IterativeWorkerTaskFactory $ 1.call(IterativeWorkerTaskFactory.java:56)       at java.util.concurrent.FutureTask.run(FutureTask.java:266)       在java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)       at java.util.concurrent.ThreadPoolExecutor $ Worker.run(ThreadPoolExecutor.java:617)       在java.lang.Thread.run(Thread.java:745)

像Weld一样,无法查找生成的类。 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>

有人能指出我解决这个问题的方法吗?我已经搜索了类似的东西,但没有成功。

1 个答案:

答案 0 :(得分:0)

以下是对您的问题的完整解释以及为什么我写的内容会修复它。

在Maven中,你至少有2个类加载器。您的测试类路径和主类路径都有自己的类加载器。根据您的依赖结构,您可以拥有其他人。在以这种方式运行时,CDI将每个类加载器标识为单独的bean归档文件。 src/main/webapp明确用于您的WAR文件。 beans.xml那里没有给你一个bean存档。向src/main/resources添加一个。此问题特定于您如何实例化焊接。

还有其他项目正确执行此操作 - CDI-unitArquillian,特别是Weld Embedded容器。如果您要使用其中之一,这不会是一个问题。