我能够部署一个RESTEasy应用程序,可以很好地使用Weld (这意味着我的CDI工作正常),但我的集成测试遇到了一些问题。我知道这个错误:
org.jboss.weld.exceptions.DeploymentException:
WELD-001408: Unsatisfied dependencies for type SomeService with qualifiers @Default同时测试:
@RunWith(WeldJUnit4Runner.class)
public class SomeServiceIT {
@Inject
private SomeService service;
@Test
public void test() {
System.out.println(service);
}
}我日志中的最后一条消息是
DEBUG::WELD-000100: Weld initialized. Validating beanssrc/test/resources/META/beans.xml的内容:
<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>顺便说一下,我尝试了cdi-unit库,它可以工作,但是我需要使用我自己的WeldJUnit4Runner,目前的WeldJUnit4Runner是:
public class WeldJUnit4Runner extends BlockJUnit4ClassRunner {
private final Weld weld;
private final WeldContainer container;
public WeldJUnit4Runner(Class<?> klass) throws InitializationError {
super(klass);
this.weld = new Weld();
this.container = weld.initialize();
}
@Override
protected Object createTest() throws Exception {
return container.instance().select(getTestClass().getJavaClass()).get();
}
}我使用weld-se 2.4.1进行测试。
谢谢。
编辑:
因此,Weld似乎只研究src/test/java (当我将SomeService复制到src/test/java时,它就工作了)。这太傻了,我不会复制我所有的课程来测试它们.如何告诉Weld从src/main/java检索类?
发布于 2017-01-27 16:10:25
因此,除了现有的src/main/resources/META-INF/beans.xml和src/test/resources/META-INF/beans.xml之外,我还可以通过创建src/main/webapp/WEB-INF/beans.xml和src/test/resources/META-INF/beans.xml来使其工作,这意味着,在同一个项目中,我有3倍于相同的文件,我觉得这很愚蠢,但我想这就是Weld世界中的情况……
谢谢你抽出时间。
编辑:
实际上,我能够只使用src/main/resources/META-INF/beans.xml部署应用程序(我删除了src/main/webapp/WEB-INF/beans.xml)
发布于 2017-01-26 07:42:54
对不起,我没有解决方案,但只有一小条线索:如果您想对BlockJUnit4ClassRunner进行一些自定义--为什么不尝试扩展org.jglue.cdiunit.CdiRunner或org.apache.deltaspike.testcontrol.api.junit.CdiTestRunner?或者至少看看他们的源代码。
Ps。我总是发现Weld的类路径扫描易碎和容易出错。尽量避免。
发布于 2017-01-27 08:08:18
它应该能用,所以我在这里张贴我所做的。
首先,我使用:
我的项目树如下:

下面是我的pom依赖项:
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-api</artifactId>
<version>7.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jboss.weld.se</groupId>
<artifactId>weld-se-core</artifactId>
<version>2.4.1.Final</version>
<scope>test</scope>
</dependency>SomeService接口:
package org.jvi.cdirunner;
public interface SomeService {
void test();
}SomeServiceImpl实现:
package org.jvi.cdirunner;
public class SomeServiceImpl implements SomeService {
@Override
public void test() {
// TODO Auto-generated method stub
}
}以及要运行的测试:
package org.jvi.cdirunner.test;
import javax.inject.Inject;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.jvi.cdirunner.SomeService;
@RunWith(WeldJUnit4Runner.class)
public class SomeServiceIT {
@Inject
private SomeService service;
@Test
public void test() {
System.out.println(service);
}
}如果我在Eclipse下运行测试,一切都会很好。我搞不懂为什么你这边不行。
https://stackoverflow.com/questions/41836690
复制相似问题