替代性CDI实施-WELD-001408:类型库的限定符@Default的不满意依赖性

时间:2018-10-07 10:25:20

标签: java java-ee java-ee-7 jboss-arquillian weld

我有一个接口Repository,有两个实现,DBRepositoryUTRepositoryUTRepository带有@Alternative注释,我正在尝试在JUnit测试中使用它。

测试类:

@RunWith(Arquillian.class)
public class UserServiceTest {
    @Inject
    private UserService service;

    @Deployment
    public static JavaArchive createDeployment() {
        return ShrinkWrap.create(JavaArchive.class)
            .addClasses(Repository.class, UserService.class, UTRepository.class)
            .addAsManifestResource(EmptyAsset.INSTANCE, "beans.xml");
    }

    @Test
    public void addCharacterTest() {
        service.addCharacter(1L, Mockito.mock(GameCharacter.class));
    }
}

被测课程:

public class UserService {
    @Inject
    Repository repository;

    @Transactional
    public void addCharacter(Long userId, GameCharacter character) {
        User user = repository.readById(userId, User.class);
        user.addCharacter(character);
        repository.update(user);
    }
}

我的beans.xml文件(放在src / test / webapp / WEB-INF / 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_2.xsd"
    bean-discovery-mode="all">
    <alternatives>
        <class>org.package.name.UTRepository</class>
    </alternatives>
</beans>

当我尝试运行测试时,出现异常:

org.jboss.weld.exceptions.DeploymentException: WELD-001408: Unsatisfied dependencies for type Repository with qualifiers @Default
    at injection point [BackedAnnotatedField] @Inject org.package.name.services.UserService.repository
    at org.package.name.services.UserService.repository(UserService.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$1.doWork(ConcurrentValidator.java:68)
    at org.jboss.weld.bootstrap.ConcurrentValidator$1.doWork(ConcurrentValidator.java:66)
    at org.jboss.weld.executor.IterativeWorkerTaskFactory$1.call(IterativeWorkerTaskFactory.java:63)
    at org.jboss.weld.executor.IterativeWorkerTaskFactory$1.call(IterativeWorkerTaskFactory.java:56)
    at java.util.concurrent.FutureTask.run(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)

出什么问题了?

2 个答案:

答案 0 :(得分:1)

您找到的解决方案效果很好,只是为了完善。

基本上,您需要添加beans.xml,这将启用您的替代方法。 EmptyAsset.INSTANCE将导致空bean.xml在这种情况下不会被剪切。

但是,在您的解决方案中,您放置在beans.xml下的src/test/webapp/WEB-INF/beans.xml没有任何作用。 如果要利用现有的beans.xml文件,可以使用:

  • addAsManifestResource(File resource, String target),其中添加给定的File,名称为target
  • addAsManifestResource(Package resourcePackage, String resourceName, String target),您可以仅指定文件所在的软件包,然后指定其名称,并在部署中指定文件的名称

答案 1 :(得分:0)

问题似乎出在.addAsManifestResource参数上。我必须将StringAsset与Arquillian将添加到beans.xml的替代方法一起使用,而不是一个空实例。

现在是createDeployment()方法:

@Deployment
public static JavaArchive createDeployment() {
    return ShrinkWrap.create(JavaArchive.class)
        .addClasses(Repository.class, UserService.class, UTRepository.class)
        .addAsManifestResource(new StringAsset("<alternatives><class>org.package.name.repositories.UTRepository</class></alternatives>"), "beans.xml");
}