这是我的服务类:
@Path("/foo")
@RequestScoped
public class FooService {
@Inject @Log
Logger logger;
@GET
@Produces(MediaType.TEXT_PLAIN)
public String ping() {
return "pong";
}
}
在同一个模块中,我有一个 util 类,它使用带有@Qualifier注释的 Log 接口提供记录器
public class WebResources {
@Produces
@RequestScoped
public FacesContext produceFacesContext() {
return FacesContext.getCurrentInstance();
}
@Produces @Log
public Logger produceLog(InjectionPoint injectionPoint) {
return Logger.getLogger(injectionPoint.getMember().getDeclaringClass().getName());
}
}
然后我使用 JUnit 5 + Weld SE 编写了一个 UnitTest
@EnableWeld
public class FooTest {
@WeldSetup
public WeldInitiator weld = WeldInitiator.from(FooService.class)
.activate(RequestScoped.class)
.build();
@Test
public void ping(FooService fooService) {
fooService.ping();
}
}
这会产生以下错误:
org.jboss.weld.exceptions.DeploymentException: WELD-001408: Unsatisfied dependencies for type Logger with qualifiers @Log
at injection point [BackedAnnotatedField] @Inject @Log it.infocert.shop.rest.FooService.logger
at it.infocert.shop.rest.FooService.logger(FooService.java:0)
如何在单元测试中正确地将 @Log 依赖项添加到 Weld SE?