Run JUnit and Spock in a maven project

background

--maven project --Slight tests were written in JUnit --It's hard to keep writing tests in JUnit --I want to write a test with Spock

Overview, what you want to do

――I want to add Spock while keeping the existing JUnit --I want to execute JUnit and Spock with one command (`` `./mvnw test```) --I don't mention how great spock is

It is a story. Dedicated to those who are still testing with JUnit in Maven ...

environment

procedure

flow

  1. For the time being, add a spock and write a test
  2. Compile groovy files with maven
  3. Make ** Spec. * File a test run target with maven

For the time being, add a spock and write a test

  1. Add dependency to pom.xml

    <dependency>
        <groupId>org.spockframework</groupId>
        <artifactId>spock-core</artifactId>
        <version>1.3-groovy-2.5</version>
        <scope>test</scope>
    </dependency>
    
  2. Write a test

    package jp.co.trech
    
    import spock.lang.*
    
    class AppSpec extends Specification {
        def 'Sample test'() {
            expect:
            Math.max(1, 2) != 2 //I want to check if the test is running, so I dare to make it a failing test
        }
    }
    
  3. Try running a test

    ./mvnw test
    → BUILD SUCCESS
    

→ No compiled file is generated & Spock is not running

Compile groovy files with maven

  1. In gradle, groovy file was compiled just by putting spock in the dependency, but maven did not compile, so add the following
  1. Try running a test

    ./mvnw test
    → BUILD SUCCESS
    

→ A compiled file has been generated! → But Spock does not run

Target ** Spec. * File for test execution with maven

  1. Add the following after the above maven-compiler-plugin

        <plugin>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>3.0.0-M1</version>
            <configuration>
                <failIfNoTests>true</failIfNoTests>
                <includes>
                    <include>**/*Test.*</include>
                    <include>**/*Spec.*</include>
                </includes>
            </configuration>
            <dependencies>
                <dependency>
                    <groupId>org.apache.maven.surefire</groupId>
                    <artifactId>surefire-junit47</artifactId>
                    <version>3.0.0-M1</version>
                </dependency>
            </dependencies>
        </plugin>
    

The version of maven-surefire-plugin depends on the version of java, so see Maven Surefire Plugin

  1. Try running a test

    ./mvnw test
    → BUILD FAILURE
    

→ A compiled file has been generated! → Spock was also executed, so the test failed!

→ Modify the Spock test and if the test passes, you're done!

Conclusion

--Spock can be easily put in gradle, but it is difficult to add Spock to maven ――Let's use gradle quietly


Reference URL

Compiling Groovy maven-surefire-plugin Using JUnit groovy/groovy-eclipse Groovy Eclipse Maven plugin

Recommended Posts

Run JUnit and Spock in a maven project
Run JUnit and Spock in a maven project
Run modular project tests in Gradle (JUnit5 + TestFX)
Run a Spring Boot project in VS Code
Until you build a project described in scala with Maven and execute it with the scala command.
Popular trends in gradle and maven
How to run JUnit in Eclipse
Output JUnit test report in Maven
How to make a Maven project
Create a Maven project with a command
Create a Spring Boot project in intellij and exit immediately after launching
Creating a project (and GitHub repository) using Java and Gradle in IntelliJ IDEA
CI for Maven project in Azure Pipelines
Add .gitignore when creating a project in Xcode
How to run a djUnit task in Ant
I made a Docker container to run Maven
[Eclipse / Tomcat] Servlet + JSP in Maven webapp project
Java tips-Create a Spring Boot project in Gradle
Add a project in any folder with Gradle
Run JVM-Math-Language as a Gradle project at hand
Create Maven Project
Until you create a Spring Boot project in Intellij and push it to Github
I want to ESLint in Maven project. I want to run Node.js without putting it in my environment.
Refactoring in JUnit
Create a Java (Maven) project with VS Code and develop it on a Docker container
[CentOS, Eclipse] Load a library file in a C project
A small story that is sometimes useful in Maven
Write a class in Kotlin and call it in Java
Display a loading image in JavaFX and then display another image
I made a Restful server and client in Spring.
How to convert A to a and a to A using AND and OR in Java
Java11: Run Java code in a single file as is