gpt4 book ai didi

java - 测试 Maven 插件时抛出 NoSuchElementException

转载 作者:搜寻专家 更新时间:2023-11-01 01:49:42 34 4
gpt4 key购买 nike

我正在尝试使用 maven plugin testing harness 测试我的 Maven 插件.我能找到的关于此事的唯一文档相当陈旧,我找到了 similar具有相同错误但没有解决方案的线程,至少不能为我解决问题。该错误可以归结为在尝试运行 lookupMojo 方法时抛出 NoSuchElementException

有没有其他人遇到过这个或类似的问题,您是如何解决的?如果您需要更多信息,请告诉我,我会发布更新。

插件类

@Mojo(name = "my_plugin", defaultPhase = LifecyclePhase.CLEAN, threadSafe = true)
public class MyPlugin extends AbstractMojo
{
private static final Logger logger = LoggerFactory.getLogger(MyPlugin.class);

@Parameter private String configFileLocation;

public void execute() throws MojoExecutionException, MojoFailureException
{
logger.info("The config file location is: {}", configFileLocation);
saveSystemProperties(new File(configFileLocation));
}

private void saveSystemProperties(final File file)
{
logger.info("Attempting to save system properties");
try(FileOutputStream fr = new FileOutputStream(file))
{
System.getProperties().store(fr, "Properties");
logger.info("Properties successfully saved. Closing File Output Stream Implicitly");
}
catch(IOException e)
{
logger.info("There was an IO error. ");
e.printStackTrace();
}
}
}

插件测试类

public class MyPluginTester extends AbstractMojoTestCase
{
private static final Logger logger = LoggerFactory.getLogger(MyPluginTester.class);

protected void setup() throws Exception
{
super.setUp();
}

protected void tearDown() throws Exception
{
super.tearDown();
}

public void testMojoGoal() throws Exception
{
logger.info("Loading Test Pom File");
File testPom = new File(getBasedir(),"src/test/resources/pom/basic-test-plugin-config.xml");
assertNotNull(testPom);
MyPlugin mojo = (LittleSysStore)lookupMojo("configure", testPom);
assertNotNull(mojo);
}
}

POM 文件

<?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>com.my.stuff</groupId>
<artifactId>my-maven-plugin</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>maven-plugin</packaging>

<properties>
<maven.plugin.annotations.version>3.5</maven.plugin.annotations.version>
<maven.plugin.testing.version>3.3.0</maven.plugin.testing.version>
<maven.version>3.5.0</maven.version>
<slf4j.version>1.7.25</slf4j.version>
<junit.version>4.12</junit.version>
<java.version>1.8</java.version>
</properties>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
</configuration>
</plugin>
</plugins>
</build>

<dependencies>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-artifact</artifactId>
<version>${maven.version}</version>
</dependency>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-compat</artifactId>
<version>${maven.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-core</artifactId>
<version>${maven.version}</version>
</dependency>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-model</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.plugin.annotations.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.maven.plugin-testing</groupId>
<artifactId>maven-plugin-testing-harness</artifactId>
<version>${maven.plugin.testing.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>${slf4j.version}</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>

输出

org.codehaus.plexus.component.repository.exception.ComponentLookupException: java.util.NoSuchElementException
role: org.apache.maven.plugin.Mojo
roleHint: com.my.stuff-maven-plugin:1.0-SNAPSHOT:clean
at org.codehaus.plexus.DefaultPlexusContainer.lookup(DefaultPlexusContainer.java:267)
at org.codehaus.plexus.DefaultPlexusContainer.lookup(DefaultPlexusContainer.java:243)
at org.codehaus.plexus.PlexusTestCase.lookup(PlexusTestCase.java:205)
at org.apache.maven.plugin.testing.AbstractMojoTestCase.lookupMojo(AbstractMojoTestCase.java:410)
at org.apache.maven.plugin.testing.AbstractMojoTestCase.lookupMojo(AbstractMojoTestCase.java:355)
at unit_tests.LittleSysStoreTest.testMojoGoal(LittleSysStoreTest.java:33)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at junit.framework.TestCase.runTest(TestCase.java:176)
at junit.framework.TestCase.runBare(TestCase.java:141)
at junit.framework.TestResult$1.protect(TestResult.java:122)
at junit.framework.TestResult.runProtected(TestResult.java:142)
at junit.framework.TestResult.run(TestResult.java:125)
at junit.framework.TestCase.run(TestCase.java:129)
at junit.framework.TestSuite.runTest(TestSuite.java:252)
at junit.framework.TestSuite.run(TestSuite.java:247)
at org.junit.internal.runners.JUnit38ClassRunner.run(JUnit38ClassRunner.java:86)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:51)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
Caused by: java.util.NoSuchElementException
at java.util.Collections$EmptyIterator.next(Collections.java:4189)
at org.codehaus.plexus.DefaultPlexusContainer.lookup(DefaultPlexusContainer.java:263)
... 23 more

最佳答案

在浪费了一个小时阅读他们可怕的文档后,我看了一下线束测试套件。尝试使用以下内容:

   void testStuff() throws Exception {
File testPom = new File(getBasedir(),"src/test/resources/pom/basic-test-plugin-config.xml");
assertNotNull(testPom);
MyPlugin mojo = new MyPlugin();
mojo = (MyPlugin) configureMojo(
mojo, extractPluginConfiguration("cue-maven-plugin", testPom
);
mojo.execute();
}

像魅力一样工作。

关于java - 测试 Maven 插件时抛出 NoSuchElementException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44009232/

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