Testing and specifically Unit Testing may seem abstract or distracting but if you presevere and simply try it
you will be rewarded with an immediate appreciation of how many little details and edge cases were overlooked.
The finished code is more robust and maintainable as every change can be validated to still produce expected results.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Create a new Project (java project) -> Create a new package (net.kittyandbear.junitexample)
Create a new class -> JunitExampleSource
package net.kittyandbear.junitexample;
public class JunitExampleSource
{
public int multiply( int x , int y )
{
return x / y;
}
}
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Right click on the Project and choose new Source Folder -> test
(or Right click on the Project -> Properties -> Java Build Path -> Source Tab -> Add Folder -> Create New Folder -> test)
Right click on the JunitExampleSource class -> JUnit Test Case (Browse to put it in the test folder) -> next to select which method to test
(Eclipse will prompt you to add JUnit to the build path) -> OK
package net.kittyandbear.junitexample;
import static org.junit.Assert.*;
import org.junit.Test;
public class JunitExampleSourceTest
{
@Test
public void testMultiply()
{
fail("Not yet implemented");
}
}
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Right click on the JunitExampleSource class -> RunAs -> JUnit Test
Next to Package Explorer a new tab "JUnit" appears with a red bar (Failures: 1) and a Failure Trace: Not yet implemented
package net.kittyandbear.junitexample;
import static org.junit.Assert.assertEquals;
import org.junit.Test;
public class JunitExampleSourceTest
{
@Test
public void testMultiply()
{
JunitExampleSource tester = new JunitExampleSource();
int result = tester.multiply( 10 , 5 );
assertEquals( "Result" , 50 , result );
}
}
java.lang.AssertionError: Result expected:<50> but was:<2>
at org.junit.Assert.fail(Assert.java:91)
the updated Test code returned an error (because the multiply function is still incorrect), fix it:
package net.kittyandbear.junitexample;
public class JunitExampleSource
{
public int multiply( int x , int y )
{ return x * y;
}
public int add( int x , int y )
{ return x + y;
}
}
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Right click on your test class -> New -> Other -> Java -> JUnit -> Test Suite
In the new AllTest.java you can control + f11 and run all tests that are listed
package net.kittyandbear.junitexample;
import org.junit.runner.RunWith;
import org.junit.runners.Suite;
import org.junit.runners.Suite.SuiteClasses;
@RunWith(Suite.class)
@SuiteClasses(
{
JunitExampleSourceMultiplyTest.class,
JunitExampleSourceAddTest.class
})
public class AllTests
{
}
@Before runs before each test , typically a default method is setUp()
@After execute the method after the execution, typically a default method is tearDown()
http://junit.org/apidocs/org/junit/Assert.html
http://code.google.com/p/econference4/wiki/HowToWriteJunitTestCases
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
When using Maven and Eclipse odd things happen
Create a new Maven Project (skip archetype selection) and once you've manually created
the Package, the pom.xml updated to use junit 4.1... BUT
import org.junit.Rule; //gets an import error
This is because of "JUnit version needs Hamcrest."
The easy way to fix this in Eclipse is
Right click on the Project -> "Configure the Build Path" -> Libraries -> Add Library -> JUnit -> 4
The errors should disappear
ALSO it requires JUnit version 4.8.2 (not 4.1)
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.8.2</version>
<scope>test</scope>
</dependency>
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
mvn clean install -DskipTests //skips running the tests
mvn clean install -Dmaven.test.skip=true //skips compiling the tests (and running them)
mvn test //compile and run the tests
mvn -Dtest=ExampleClassName //to run a single test file
mvn -Dtest=ExampleClassName#testMethod //to run a single test method in a test file
-Dtest=MyTest
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
In order to read a file (e.g. java properties file) from src/test/resources
1. create the resources directory with a right click in eclipse
2. copy or create the file in the resources directory (ensure you refresh the project in eclipse!)
3. in your unit test access the file with:
InputStream inputStream = getClass().getResourceAsStream("/test.app.properties");
Properties appProperties = new Properties();
appProperties.load(inputStream);
assertEquals( "expectedValue" , appProperties.getProperty( "expectedKey" ) );
private static final String TESTAPPPROPERTIESRESOURCE = "authgateway.app.properties";
...
testResourceUrl = this.getClass().getClassLoader().getResource( TESTAPPPROPERTIESRESOURCE );
File f = new File( testResourceUrl.getFile() );
testerDefault = new PropertiesWriter( f.getAbsolutePath() );