java - 如何使用模拟覆盖@Produces

标签 java unit-testing mockito cdi java-ee-7

我有一个类,它生成一个 ElasticSearch 客户端以与 @Inject 一起使用

@Produces
@ApplicationScoped
public Client createClient() {
    return new TransportClient().addTransportAddress(new InetSocketTransportAddress(IP, 9300));
}

我想在我的单元测试中使用如下内容来模拟这个客户端

@Produces
@Mock
private Client client;

这会导致 AmbigouslyResolutionException,因为同一个 bean 有两个提供者。

如何确定仅用于单元测试的模拟生成类的优先级?

<小时/>

根据下面的评论,我做了以下更改。我在替代方案中设置了一个断点,但它没有被击中。

ElasticSearchProducer.java

@ApplicationScoped
public class ElasticSearchProducer {

    public static final String IP = "10.9.215.28";

    private Client client;

    @PostConstruct
    public void init() {
        client = createClient();
    }

    protected Client createClient() {
        return new TransportClient().addTransportAddress(new InetSocketTransportAddress(IP, 9300));
    }

    @Produces
    @ApplicationScoped
    public Client getClient() {
        return client;
    }
}

ElasticSearchProducerAlternative.java

@Alternative
@ApplicationScoped
public class ElasticSearchProducerAlternative extends ElasticSearchProducer {

    @Override
    public Client createClient() {
        return Mockito.mock(Client.class);
    }
}

测试-beans.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://java.sun.com/xml/ns/javaee"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/beans_1_0.xsd">

    <alternatives>
        <class>com.walmart.platform.pv.cdi.ElasticSearchProducerAlternative</class>
    </alternatives>

</beans>

ArquillianTest.java

@RunWith(Arquillian.class)
public abstract class ArquillianTest {
    @Deployment(testable = true)
    public static WebArchive createDeployment() {


        WebArchive war = ShrinkWrap.create(WebArchive.class, "quintessence.war")
                .addPackages(true, "com.w.platform");

        war.merge(ShrinkWrap.create(GenericArchive.class).as(ExplodedImporter.class)
                        .importDirectory("src/main/webapp").as(GenericArchive.class),
                "/", Filters.includeAll());

        war.addAsManifestResource("etc/conf/test/import.sql");
        war.addAsManifestResource("test-persistence.xml", "persistence.xml");
        war.addAsWebInfResource("test-beans.xml", "beans.xml");
        war.addAsWebInfResource("test-resources.xml", "resources.xml");

        System.out.println(war.toString(true));
        return war;
    }
}

在我的测试中,未使用替代类

最佳答案

您需要记住始终编写可测试的代码。假设你的类是这样写的

@ApplicationScoped
public class ESClient {
    @Produces
    @ApplicationScoped
    public Client createClient() {
        return new TransportClient().addTransportAddress(new InetSocketTransportAddress(IP, 9300));
    }
}

这可行,但不太可测试。您可能想使用模拟进行单元测试,没有 Activity 连接。我们可以重构它以使其更具可测试性。考虑这个替代实现:

@ApplicationScoped
public class ESClient {
    private Client client;
    @PostConstruct
    public void init() {
         this.client = createClient();
    }
    @Produces
    @ApplicationScoped
    public Client getClient() {
        return this.client;
    }
    protected Client createClient() {
        return new TransportClient().addTransportAddress(new InetSocketTransportAddress(IP, 9300));
    }
}

是的,它有点冗长,但你的关注点被更好地分开了。然后,您可以在测试中提供替代实现

@ApplicationScoped
@Alternative
public class MockESClient extends ESClient {
    @Override
    protected Client createClient() {
        // return your mock here
    }
}

现在您只需在 beans.xml 文件中激活此替代方案即可。

关于java - 如何使用模拟覆盖@Produces,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31551932/

相关文章:

java - MVC 架构中的 Rx

java - 数组的初始化

python - BeautifulSoup 在标签中寻找标签

java - 返回 NullPointerException 的模拟方法

android - 为什么我的 Android 单元测试一起运行时失败,但单独运行时通过?

java - 在 Java 中第一次匹配之前使用正则表达式的子字符串

java - 在 Linux 上运行 WEKA

c# - 使用 Microsoft EntityFramework 进行单元测试

python - 如何为 Django 测试自动创建 postgis 数据库?

java - Mockito 未返回预期对象时