加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

java – 测试Maven插件时抛出NoSuchElementException

发布时间:2020-12-15 04:38:41 所属栏目:Java 来源:网络整理
导读:我正在尝试使用 maven plugin testing harness 来测试我的maven插件.我能找到的关于这个问题的唯一文档相当陈旧,我发现 similar 线程有相同的错误,但没有解决方案,至少没有一个能解决我的问题.在尝试运行lookupMojo方法时,错误可以归结为抛出NoSuchElementEx
我正在尝试使用 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();
    }

工作就像一个魅力.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读