Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Code Block
.
├── pom.xml
└── src
    ├── main
    │   ├── java
    │   │   └── SourceCodeClass.java
    │   └── resources
    └── test
        └── java
            └── TestClass.java


pom.xml

There is  pom.xml file, which is config file for our Java project.

According to  https://maven.apache.org/guides/introduction/introduction-to-the-pom.html 

A Project Object Model or POM is the fundamental unit of work in Maven. It is an XML file that contains information about the project and configuration details used by Maven to build the project. It contains default values for most projects. Examples for this is the build directory, which is target; the source directory, which is src/main/java; the test source directory, which is src/test/java; and so on. When executing a task or goal, Maven looks for the POM in the current directory. It reads the POM, gets the needed configuration information, then executes the goal.

Some of the configuration that can be specified in the POM are the project dependencies, the plugins or goals that can be executed, the build profiles, and so on. Other information such as the project version, description, developers, mailing lists and such can also be specified.


The simplest example of pom.xml that enables running tests in IDE and shell looks like this:

...

Code Block
<?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>org.example</groupId>
    <artifactId>JUnit5_Overview<</artifactId>
    <version>1.0-SNAPSHOT</version>

// This is depedency that we need to add to usew JUnit5 in our project
    <dependencies>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter</artifactId>
            <version>5.7.2</version>
            <scope>test</scope>
        </dependency>
    </dependencies>


    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>11</maven.compiler.source>
        <maven.compiler.target>11</maven.compiler.target>
    </properties>


// This plugins enables running tests from shell
    <build>
        <plugins>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.1</version>
            </plugin>
            <plugin>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>3.0.0-M1</version>
            </plugin>
        </plugins>
    </build>

</project>


main / test folders


Inside Overview/src  folder you can see two subfolders:

  • main - here are our source codes, with working functionalities →  SourceCodeClass.java 

  • test - here are test classes, that check whether source code is working properly. This is where our JUnit5 is used → TestClass.java 



Running tests


From shell

Note

Remember to install Maven & Java 8!


Go to your project directory 

...

Code Block
 
[INFO] Scanning for projects...
[INFO]
[INFO] -----------------------< org.example:Calculator >-----------------------
[INFO] Building Calculator 1.0-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ Calculator ---
[INFO] Deleting /home/agatafilipczak/workspace/ACH/ach-tutorials/TDD-java/Calculator/target
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ Calculator ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory /home/agatafilipczak/workspace/ACH/ach-tutorials/TDD-java/Calculator/src/main/resources
[INFO]
[INFO] --- maven-compiler-plugin:3.8.1:compile (default-compile) @ Calculator ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 1 source file to /home/agatafilipczak/workspace/ACH/ach-tutorials/TDD-java/Calculator/target/classes
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ Calculator ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory /home/agatafilipczak/workspace/ACH/ach-tutorials/TDD-java/Calculator/src/test/resources
[INFO]
[INFO] --- maven-compiler-plugin:3.8.1:testCompile (default-testCompile) @ Calculator ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 1 source file to /home/agatafilipczak/workspace/ACH/ach-tutorials/TDD-java/Calculator/target/test-classes
[INFO]
[INFO] --- maven-surefire-plugin:3.0.0-M1:test (default-test) @ Calculator ---
[INFO]

//This is important part, we can see that 2 tests were run, no errors and build success!

[INFO] -------------------------------------------------------
[INFO]  T E S T S
[INFO] -------------------------------------------------------
[INFO] Running CalculatorTest
[INFO] Tests run: 2, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.041 s - in CalculatorTest
[INFO]
[INFO] Results:
[INFO]
[INFO] Tests run: 2, Failures: 0, Errors: 0, Skipped: 0
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  2.819 s
[INFO] Finished at: 2021-10-13T19:07:48+02:00
[INFO] ------------------------------------------------------------------------


From Intellij IDE


Panel

After we have set up the code for the testing, we can run the tests and find out if the tested methods are working correctly.

  • To run an individual test, click Icons run configurations test state runImage Modified in the gutter and select Run.

Image Added


  • To run all tests in a test class, click Icons run configurations test state runImage Modified against the test class declaration and select Run.

Image Added


You can view test results in the Run tool window.

Image Modified