【问题标题】:How to use mockito with maven testing harness如何在 Maven 测试工具中使用 mockito
【发布时间】:2020-06-28 07:33:04
【问题描述】:

我正在开发的 maven 插件现在使用带有 JSR-330 的 Google Guice 注入一些依赖项。对于单元测试,我使用maven-plugin-testing-harness。该插件完美运行。但是测试有问题。我想将模拟组件注入 mojo,但测试中仍然有真实的对象。

我尝试按照Google Guice Bound Field 中的说明编写自定义测试模块,但没有成功。经过一些调试,我发现 Plexus 容器不允许使用自定义模块。

有我的魔力:

package my.maven.plugins.myplugin;

import my.maven.plugins.myplugin.component.MyComponent;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.project.MavenProject;

import javax.inject.Inject;

@Mojo(name = "resolve-property-value")
public class MyMojo extends AbstractMojo {

    private final MyComponent component;

    @Parameter(readonly = true, defaultValue = "${project}" )
    private MavenProject project;

    @Inject
    public MyMojo(MyComponent component) {
        this.component = component;
    }

    @Override
    public void execute() {
        String value = component.resolvePropertyValue();
        project.getProperties().setProperty("some.property", value);
    }
}

组件的接口:

package my.maven.plugins.myplugin.component;

public interface MyComponent {

    String resolvePropertyValue();
}

及实施

package my.maven.plugins.myplugin.component.impl;

import my.maven.plugins.myplugin.component.MyComponent;

import javax.inject.Named;

@Named
public class MyComponentImpl implements MyComponent {

    @Override
    public String resolvePropertyValue() {
        return "someValue";
    }
}

测试:

package my.maven.plugins.myplugin;

import my.maven.plugins.myplugin.component.MyComponent;
import org.apache.maven.plugin.Mojo;
import org.apache.maven.plugin.testing.MojoRule;
import org.apache.maven.project.MavenProject;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;

import java.util.Properties;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.doReturn;

public class MyMojoTest {

    @Mock
    private MyComponent component;

    @Rule
    public MojoRule mojoRule = new MojoRule();

    @Before
    public void setUp() {
        MockitoAnnotations.initMocks(this);
    }

    @Test
    public void should_set_some_property() throws Exception {
        doReturn("testValue").when(component).resolvePropertyValue();
        MavenProject project = new MavenProject();
        Mojo goal = mojoRule.lookupConfiguredMojo(project, "resolve-property-value");
        goal.execute();
        Properties properties = project.getProperties();
        assertTrue(properties.containsKey("some.property"));
        assertEquals("testValue", properties.get("some.property"));
    }
}

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>my.maven.plugins</groupId>
    <artifactId>my-plugin</artifactId>
    <version>0.1.0-SNAPSHOT</version>
    <packaging>maven-plugin</packaging>

    <properties>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

        <maven.version>3.6.0</maven.version>
        <maven-test.version>3.3.0</maven-test.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.apache.maven</groupId>
            <artifactId>maven-core</artifactId>
            <version>${maven.version}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.maven</groupId>
            <artifactId>maven-plugin-api</artifactId>
            <version>${maven.version}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.maven.plugin-tools</groupId>
            <artifactId>maven-plugin-annotations</artifactId>
            <version>${maven.version}</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>com.google.guava</groupId>
            <artifactId>guava</artifactId>
            <version>21.0</version>
        </dependency>
        <dependency>
            <groupId>org.apache.maven.resolver</groupId>
            <artifactId>maven-resolver-api</artifactId>
            <version>1.3.1</version>
        </dependency>
        <dependency>
            <groupId>org.apache.maven</groupId>
            <artifactId>maven-compat</artifactId>
            <version>${maven.version}</version>
            <scope>test</scope>
        </dependency>

        <!-- Test dependencies -->
        <dependency>
            <groupId>org.apache.maven.plugin-testing</groupId>
            <artifactId>maven-plugin-testing-harness</artifactId>
            <version>${maven-test.version}</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.mockito</groupId>
            <artifactId>mockito-core</artifactId>
            <version>2.23.4</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <pluginManagement>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-plugin-plugin</artifactId>
                    <version>${maven.version}</version>
                </plugin>
            </plugins>
        </pluginManagement>
    </build>
</project>

有没有办法在插件单元测试中使用 mockito?

【问题讨论】:

  • 我没有看到任何可以将 component 模拟提供给 MyMojo 类的代码。 mojoRule 的对象构造应该如何自行完成?
  • 这是我的问题。我找不到任何指南或示例如何做到这一点。我尝试使用模拟绑定创建自定义 guice 模块,但没有成功。我不确定是否可以在这里使用 mockito。但我想因为它是一个单元测试,所以应该有一个解决方案来替换真正的组件
  • @second,谢谢。我试图找到一些隐含的方法来注入模拟。但是在您发表评论后,我对 Plexus 容器的方法进行了另一项研究,并在我的答案中找到了该方法。我对解决方案不满意,但会坚持下去。

标签: java maven mockito maven-plugin maven-plugin-testing-harness


【解决方案1】:

我想没有“神奇”的方式来注入模拟组件。所以我决定在 setUp 部分明确地将它放入容器中

@Before
public void setUp() {
    MockitoAnnotations.initMocks(this);
    mojoRule.getContainer().addComponent(component, MyComponent.class, "");
}

【讨论】:

  • 除非你可以控制对象的创建,否则你不能真正使用mockito默认提供的“魔法”。作为后备reflections 始终是一种选择,但是这种方法看起来更好。
猜你喜欢
  • 2021-05-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-08-12
  • 2014-03-30
  • 2021-04-27
相关资源
最近更新 更多