java - @Qualifier 在 Junit4 中不起作用

标签 java spring junit

我尝试通过阅读 Spring in action 4 来提高我的 Spring 知识。 当我必须进行部分描述限定符注释(3.3.2)的使用时,我遇到了问题。 为了测试这个注释的实际效果,我编写了 Dessert 接口(interface),它由 3 个类实现,使用 @Component 注释在上下文中创建。 我还创建了 Taster 类,它“品尝”一些甜点,由一些限定符自动连接到其中。 当我使用 AnnotationConfigApplicationContext 运行我的应用程序时 - 一切正常。对于 SpringJUnit4ClassRunner - 则不然。我想我在测试代码中遗漏了一些东西,但我没有足够的知识来意识到什么。 接口(interface):

package bakery.intrface;
@FunctionalInterface
public interface Dessert {
    void introduce();
}

蛋糕:

package bakery.desserts;

import bakery.intrface.Dessert;
import org.springframework.stereotype.Component;

@Component
public class Cake implements Dessert {
    @Override
    public void introduce() {
        System.out.println("I am a cake!");
    }
}

Cookie:

package bakery.desserts;

import bakery.intrface.Dessert;
import org.springframework.stereotype.Component;

@Component
public class Cookie implements Dessert {
    @Override
    public void introduce() {
        System.out.println("I'm a cookie!");
    }
}

冰淇淋:

package bakery.desserts;

import bakery.intrface.Dessert;
import org.springframework.stereotype.Component;

@Component
public class IceCream implements Dessert {
    @Override
    public void introduce() {
        System.out.println("I'm an ice cream!");
    }
}

类(class),消耗一些 bean ,品尝者:

package bakery;

import bakery.intrface.Dessert;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

@Component
public class Taster {

    private Dessert dessert;

    public void taste(){
        dessert.introduce();
    }

    @Autowired
    @Qualifier("iceCream")
    public void setDessert(Dessert dessert) {
        this.dessert = dessert;
    }
}

配置:

package bakery.config;

import bakery.Bakery;
import bakery.Taster;
import bakery.desserts.Cake;
import bakery.intrface.Dessert;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;

@ComponentScan(basePackageClasses = Bakery.class)
public class BakeryConfig {
}

运行类:

package bakery;

import bakery.config.BakeryConfig;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class Bakery {
    public static void main(String[] args) {
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(BakeryConfig.class);
        String[] beans  = context.getBeanDefinitionNames();
        Taster taster = (Taster) context.getBean("taster");
        taster.taste();
    }
}

测试类:

package bakery;

import bakery.config.BakeryConfig;
import bakery.intrface.Dessert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import static org.junit.Assert.assertNotNull;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = BakeryConfig.class)
public class BakeryTest {

    @Autowired
    Dessert dessert;

    @Autowired
    Taster taster;

    @Test
    public void contextInit(){
        assertNotNull(dessert);
        dessert.introduce();
    }

    @Test
    public void tasterInit(){
        assertNotNull(taster);
    }
}

当我运行测试时,出现异常:没有定义 [bakery.intrface.Dessert] 类型的合格 bean:预期有单个匹配 bean,但发现了 3 个:cookie、iceCream、cake。

最佳答案

您的应用程序上下文中有 3 个“Dessert”bean,您必须指定要连接哪一个。

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = BakeryConfig.class)
public class BakeryTest {

    @Autowired
    @Qualifier("iceCream")  // <===================== you must specify which bean to be wired
    Dessert dessert;

    @Autowired
    Taster taster;

关于java - @Qualifier 在 Junit4 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36788366/

相关文章:

java - Assert.assertEquals(object 1, object2) 如何比较两个对象?

java - 如何在 jUnit 测试中使用@Autowired?

java - 有什么方法可以在 GWT 中创建类似于 Javascript 警报的东西吗?

java - 我一直在尝试在 map 下面获取底部表格,但收到错误,我不知道它来自哪里

java - 如何在 Java 中为 REST 服务获取 WSDL

java - 如何一次获取 500 行,直到获取表中的所有项目?

java - 在没有 Spring 的情况下在 aop.xml 中组合 AOP 切入点

java - 如何在单元测试(junit)中管理巨大的类和重构

java - 在 MVC 模式中将模型和操作划分为类的最佳方法是什么

java - 在 addListener 中从 ListView 中删除项目