Versions Compared

Key

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

...

  • 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 

...

From shell

Note

Remember to install Maven & Java 8!

Go to your project directory 

Code Block
> cd <project dir>

and then run this:

Code Block
> mvn clean test

// clean deletes caches

You shoud see sth like this this:

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

...

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 all tests in a test class, click Icons run configurations test state runImage Removed against the test class declaration and select Run.
  • To run an individual test, click Icons run configurations test state runImage Removed in the gutter and select Run.

Image Removed

You can view test results in the Run tool window at the bottom of IDE.

  • functionalities 

    Code Block
    public class SourceCodeClass {
         
     
        public static void main(String[] args) {
            System.out.printf("This is our main class!");
        }
     
    }


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

    Code Block
    // We import jupiter to our class to use proper annotation
    import org.junit.jupiter.api.*;
    
    @DisplayName("JUnit 5 Example class")
    class TestClass {
    
        @Test
        void exampleTest() {
            System.out.println("example test method");
        }
    
    }

...