## page was renamed from JUnit
= JUnit =
A programmer-oriented testing framework for Java.
http://junit.org/
== Annotations ==
* @Before , public void setUp(){} // runned before each test
* @Test , public void textAbc(){} // test itself
* @After , public void tearDown(){} // runned before each test
* @BeforeClass
* @AfterClass
* @Ignore, @Ignore("Not yet implemented")
== Imports ==
{{{#!highlight java
import static org.junit.Assert.assertEquals;
import org.junit.Test;
import org.junit.Ignore;
import org.junit.runner.RunWith;
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
{{{
org.apache.maven.plugins
maven-surefire-plugin
**/Test*.java
**/*Test.java
**/*TestCase.java
}}}
== Import methods ==
{{{#!highlight java
import static org.junit.Assert.*;
// ...
assertEquals(...);
}}}
== Maven dependency ==
{{{
junit
junit
4.11
}}}
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 ==
{{{#!highlight bash
cd ~
mkdir junitSample1
cd junitSample1/
nano pom.xml
mkdir -p src/main/java/org/allowed/bitarus/junitSample1
mkdir -p src/test/java/org/allowed/bitarus/junitSample1
nano src/main/java/org/allowed/bitarus/junitSample1/OddEven.java
nano src/test/java/org/allowed/bitarus/junitSample1/TestOddEven.java
mvn clean compile test
mvn -Dtest=TestOddEven#testEven1 test # only run testEven1
}}}
Structure:
{{{
.
|-- pom.xml
|-- src
| |-- main
| | `-- java
| | `-- org
| | `-- allowed
| | `-- bitarus
| | `-- junitSample1
| | `-- OddEven.java
| `-- test
| `-- java
| `-- org
| `-- allowed
| `-- bitarus
| `-- junitSample1
| `-- TestOddEven.java
}}}
'''pom.xml'''
{{{#!highlight xml
4.0.0
org.allowed.bitarus
junitSample1
0.0.1
jar
junit
junit
4.11
test
}}}
'''src/main/java/org/allowed/bitarus/junitSample1/OddEven.java'''
{{{#!highlight java
package org.allowed.bitarus.junitSample1;
public class OddEven
{
public static boolean isOdd(int value){
if( value % 2 == 0) return false;
else return true;
}
public static boolean isEven(int value){
//if( value % 2 == 0) return true;
//else return false;
throw new UnsupportedOperationException();
}
}
}}}
'''src/test/java/org/allowed/bitarus/junitSample1/TestOddEven.java'''
{{{#!highlight java
package org.allowed.bitarus.junitSample1.tests;
import org.allowed.bitarus.junitSample1.OddEven;
import static org.junit.Assert.assertEquals;
import org.junit.Test;
import org.junit.Ignore;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
public class TestOddEven{
@Test
public void testOdd1(){
assertEquals( OddEven.isOdd(1) , true );
assertEquals( OddEven.isOdd(4) , false );
}
@Test
public void testEven1(){
assertEquals( OddEven.isEven(4) , true );
assertEquals( OddEven.isEven(1) , false );
}
}
}}}