gpt4 book ai didi

java - 如何将mockito与maven测试工具一起使用

转载 作者:行者123 更新时间:2023-12-02 08:52:01 27 4
gpt4 key购买 nike

我现在正在开发的 Maven 插件使用 Google Guice 和 JSR-330 注入(inject)一些依赖项。对于单元测试,我使用 maven-plugin-testing-harness 。该插件运行完美。但测试有问题。我想将模拟组件注入(inject)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);
}
}

组件的接口(interface):

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?

最佳答案

我认为没有“神奇”的方法来注入(inject)模拟组件。所以我决定在设置部分将其显式放入容器中

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

关于java - 如何将mockito与maven测试工具一起使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60718662/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com