NoSuchElementException exception when testing Maven plugin

I am trying to test my maven plugin using the module to test the maven plugin . The only documentation I can find on this issue is quite old, and I found similar threads with the same error, but without permission, at least not the one that solves the problem for me. The error can be reduced to a NoSuchElementException when trying to run the lookupMojo method.

Does anyone else experience this or a similar problem, and how did you fix it? Let me know if you need more information and I will post updates.

Plugin class

@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 Testing Class

 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 file

 <?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> 

Output

 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 
+3
source share
1 answer

Having missed an hour, reading their scary documentation, I looked at a set of test tests. Try using the following:

  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(); } 

Worked like a charm.

+6
source

All Articles