У меня есть интерфейс Repository
с двумя реализациями, DBRepository
и UTRepository
.UTRepository
помечено @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)
В чем проблема?