Ich versuche, mein Maven-Plugin mit der maven plugin testing harness zu testen. Die einzige Dokumentation, die ich zu diesem Thema finden kann, ist ziemlich alt, und ich habe similar Threads mit dem gleichen Fehler gefunden, aber keine Auflösung, zumindest nicht eine, die das Problem für mich löst. Der Fehler kann auf NoSuchElementException ausgekocht werden, wenn versucht wird, die Methode lookupMojo auszuführen.NoSuchElementException beim Testen des Maven-Plugins geworfen
Hat jemand anderes dieses oder ein ähnliches Problem, und wie haben Sie es behoben? Lassen Sie mich wissen, wenn Sie weitere Informationen benötigen und ich werde Updates posten.
Plugin Klasse
@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();
}
}
}
Plugin Testklasse
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 Datei
<?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>
Ausgabe
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
Wie kennt Ihr MyPluginTester diese Methode? Enthält die Klasse "AbstractMojoTestCase" die Methode "lookupMojo"? Vielleicht ist die Methode privat statt öffentlich? –
korrekt. AbstractMojoTestCase ist eine Klasse, die im Paket org.apache.maven.plugin.testing enthalten ist. Es sollte alles enthalten, was für die Durchführung des Tests notwendig ist, oder so, wie es den verknüpften Dokumenten entspricht. – tramstheman
sie sind wirklich, und aus dem, was ich bei der Suche dieses Problem gesammelt habe, ist es ein häufiges Problem und ohne eine dokumentierte Auflösung – tramstheman