JUnit

A programmer-oriented testing framework for Java.

http://junit.org/

Annotations

Imports

   1 import static org.junit.Assert.assertEquals;
   2 import org.junit.Test;
   3 import org.junit.Ignore;
   4 import org.junit.runner.RunWith;
   5 import org.junit.runners.JUnit4;

Inclusion classes

http://maven.apache.org/surefire/maven-surefire-plugin/examples/inclusion-exclusion.html

By default, the Surefire Plugin will automatically include all test classes with the following wildcard patterns:

"**/Test*.java" - includes all of its subdirectories and all java filenames that start with "Test".
"**/*Test.java" - includes all of its subdirectories and all java filenames that end with "Test".
"**/*TestCase.java" - includes all of its subdirectories and all java filenames that end with "TestCase".

Plugin configuration in pom.xml

                        <plugin>
                                <groupId>org.apache.maven.plugins</groupId>
                                <artifactId>maven-surefire-plugin</artifactId>
                                <configuration>
                                        <includes>
                                                <include>**/Test*.java</include>
                                                <include>**/*Test.java</include>
                                                <include>**/*TestCase.java</include>
                                        </includes>
                                </configuration>
                        </plugin>

Import methods

   1  import static org.junit.Assert.*;
   2   //  ...
   3   assertEquals(...);

Maven dependency

<dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>4.11</version>
</dependency>  

On a Maven project the tests may be located inside /src/test/java/ so they are integrated in the main artifact generated by Maven.

Sample project junitSample1

   1 cd ~
   2 mkdir junitSample1
   3 cd junitSample1/
   4 nano pom.xml
   5 mkdir -p src/main/java/org/allowed/bitarus/junitSample1
   6 mkdir -p src/test/java/org/allowed/bitarus/junitSample1
   7 nano src/main/java/org/allowed/bitarus/junitSample1/OddEven.java
   8 nano src/test/java/org/allowed/bitarus/junitSample1/TestOddEven.java
   9 mvn clean compile test
  10 mvn -Dtest=TestOddEven#testEven1 test # only run testEven1
  11 

Structure:

.
|-- pom.xml
|-- src
|   |-- main
|   |   `-- java
|   |       `-- org
|   |           `-- allowed
|   |               `-- bitarus
|   |                   `-- junitSample1
|   |                       `-- OddEven.java
|   `-- test
|       `-- java
|           `-- org
|               `-- allowed
|                   `-- bitarus
|                       `-- junitSample1
|                           `-- TestOddEven.java

pom.xml

   1 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
   2 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
   3   <modelVersion>4.0.0</modelVersion>
   4   <groupId>org.allowed.bitarus</groupId>
   5   <artifactId>junitSample1</artifactId>
   6   <version>0.0.1</version>
   7   <packaging>jar</packaging>
   8   <dependencies>
   9     <dependency>
  10       <groupId>junit</groupId>
  11       <artifactId>junit</artifactId>
  12       <version>4.11</version>
  13       <scope>test</scope>
  14     </dependency> 
  15   </dependencies>
  16 </project> 

src/main/java/org/allowed/bitarus/junitSample1/OddEven.java

   1 package org.allowed.bitarus.junitSample1;
   2 
   3 public class OddEven
   4 {
   5   public static boolean isOdd(int value){
   6     if( value % 2 == 0) return false;
   7     else return true; 
   8     
   9   }
  10 
  11   public static boolean isEven(int value){
  12     //if( value % 2 == 0) return true;
  13     //else return false; 
  14     throw new UnsupportedOperationException();
  15   }
  16 }

src/test/java/org/allowed/bitarus/junitSample1/TestOddEven.java

   1 package org.allowed.bitarus.junitSample1.tests;
   2 import org.allowed.bitarus.junitSample1.OddEven;
   3 
   4 import static org.junit.Assert.assertEquals;
   5 import org.junit.Test;
   6 import org.junit.Ignore;
   7 import org.junit.runner.RunWith;
   8 import org.junit.runners.JUnit4;
   9 
  10 public class TestOddEven{
  11     @Test
  12     public void testOdd1(){
  13         assertEquals( OddEven.isOdd(1) , true );
  14         assertEquals( OddEven.isOdd(4) , false );
  15     }
  16 
  17     @Test
  18     public void testEven1(){
  19         assertEquals( OddEven.isEven(4) , true );
  20         assertEquals( OddEven.isEven(1) , false );
  21     }
  22 
  23 }

Java/JUnit (last edited 2022-08-25 08:58:49 by localhost)