简体   繁体   中英

Adding Groovy Tests to Java Maven Project

I have been using Jsoup in my Groovy scripts to parse html pages. The script includes the Jsoup library by using grapes.

However, I ran into a bug and wanted to fix it. I was able to repeat the bug within the Groovy script. I tried to replicate the bug by adding a Java test to the project, however the test passed and I wasn't able to get any useful information.

I want to replicate the bug within the project by writing a tests in Groovy. However, I'm not sure what changes I need to make to pom.xml in order to include and run Groovy tests. Any help appreciated.

The method I used was to include GMavenPlus into the project. The first step involves making the necessary changes to pom.xml . So add the following to the <plugins> section:

<plugin>
    <groupId>org.codehaus.gmavenplus</groupId>
    <artifactId>gmavenplus-plugin</artifactId>
    <version>1.5</version>
    <executions>
        <execution>
            <goals>
                <goal>addSources</goal>
                <goal>addTestSources</goal>
                <goal>compile</goal>
                <goal>testCompile</goal>
            </goals>
        </execution>
    </executions>
</plugin>
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
</plugin>
<plugin>
    <!-- if including source jars, use the no-fork goals
       otherwise both the Groovy sources and Java stub sources will get included in your jar -->
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-source-plugin</artifactId>
    <!-- source plugin \> = 2.1 is required to use the no-fork goals -->
    <version>2.4</version>
    <executions>
        <execution>
            <id>attach-sources</id>
            <goals>
                <goal>jar-no-fork</goal>
                <goal>test-jar-no-fork</goal>
            </goals>
        </execution>
    </executions>
</plugin>

Next add the following to the <dependencies> section:

<dependency>
    <groupId>org.codehaus.groovy</groupId>
    <artifactId>groovy-all</artifactId>
    <version>2.4.3</version>
</dependency>

Next all create the appropriate test directory and test class:

mkdir src/test/groovy
mkdir -p src/test/groovy/org/jsoup/integration
touch src/test/groovy/org/jsoup/integration/GroovyTest.groovy

Then I added tests to GroovyTest.groovy and I was able to execute the tests.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM