WELD-001408使用Arquillian在注入点[[field])上带有限定符[@Default]的[Logger]类型的依赖关系不令人满意 [英] WELD-001408 Unsatisfied dependencies for type [Logger] with qualifiers [@Default] at injection point [[field] using arquillian
问题描述
我正在使用基础站点上的Greeter示例运行基础的基础单元测试.唯一的区别是,正在Greeter.java中的greet(PrintStream to,String name)函数中执行log.debug.我正在使用slf4j进行记录.
I am running a basic arquillian unit test, using the Greeter example on the arquillian site. The only difference is that am doing a log.debug in the greet(PrintStream to, String name) function in Greeter.java. Am using slf4j for logging.
Greeter.java
Greeter.java
package org.arquillian.example;
import java.io.PrintStream;
import javax.inject.Inject;
import org.slf4j.Logger;
public class Greeter {
@Inject
private Logger log;
public void greet(PrintStream to, String name) {
log.debug("Greeter Testing");
to.println(createGreeting(name));
}
public String createGreeting(String name) {
return "Hello, " + name + "!";
}
}
GreeterTest.java
GreeterTest.java
package org.arquillian.example;
import javax.inject.Inject;
import org.jboss.arquillian.container.test.api.Deployment;
import org.jboss.arquillian.junit.Arquillian;
import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.jboss.shrinkwrap.api.asset.EmptyAsset;
import org.jboss.shrinkwrap.api.spec.JavaArchive;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
@RunWith(Arquillian.class)
public class GreeterTest {
@Inject
Greeter greeter;
@Deployment
public static JavaArchive createDeployment() {
return ShrinkWrap.create(JavaArchive.class)
.addClass(Greeter.class)
.addAsManifestResource(EmptyAsset.INSTANCE, "beans.xml");
}
@Test
public void should_create_greeting() {
Assert.assertEquals("Hello, Earthling!",
greeter.createGreeting("Earthling"));
greeter.greet(System.out, "Earthling");
}
}
在运行测试时,在注入点[[field] @Inject private org.arquillian.example.Greeter.log]上,带有限定符[@Default]的类型[Logger]的类型[Logger]的依赖项未得到满足.有人可以帮忙吗?
Am getting WELD-001408 Unsatisfied dependencies for type [Logger] with qualifiers [@Default] at injection point [[field] @Inject private org.arquillian.example.Greeter.log] error when running the test. Can someone please help on this?
推荐答案
这是CDI问题.首先,您没有Logger
的生产者.
This is a CDI issue. You don't have a producer for your Logger
in the first place.
第二,任何此类生产者都应添加到ShrinkWrap部署中.
Secondly, any such producer should be added to the ShrinkWrap deployment.
记录器的生产者通常是这样写的:
A producer for the Logger is usually written as such:
import javax.enterprise.inject.Produces;
import javax.enterprise.inject.spi.InjectionPoint;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class SLF4JProducer {
@Produces
public Logger producer(InjectionPoint ip){
return LoggerFactory.getLogger(
ip.getMember().getDeclaringClass().getName());
}
}
此生产者接收注入点,然后继续返回SLF4J Logger
实例.该实例与包含注入点的类具有相同的名称.
This producer receives an injection point and proceeds to return a SLF4J Logger
instance. The instance has the same name as the class containing the injection point.
这篇关于WELD-001408使用Arquillian在注入点[[field])上带有限定符[@Default]的[Logger]类型的依赖关系不令人满意的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!