= JUnit =
A programmer-oriented testing framework for Java.
http://junit.org/
== Annotations ==
* @Before , public void setUp(){}
* @Test , public void textAbc(){}
* @After , public void tearDown(){}
== 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".
}}}
== 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 ==
{{{
.
|-- 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.TestCase;
import org.junit.Ignore;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
//@TestCase
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 );
}
}
}}}