test
http://www.vogella.com/tutorials/JUnit/article.html#exercise-using-junit
1. Testing
1.1 Test fixture
A test fixture is a fixes state in code which is tested used as input for a test. Another way to describe this is a test precondition.
Fixturesis a fixed state of a set of objects used as a baseline for running tests. The purpose of a test fixture is to ensure that there is a well-known and fixed environment in which tests are run so that results are repeatable. It includes −
setUp() method, which runs before every test invocation.
tearDown() method, which runs after every test method.
import junit.framework.*;
public class JavaTest extends TestCase {
protected int value1, value2;
// assigning the values
protected void setUp(){
value1 = 3;
value2 = 3;
}
// test method to add two values
public void testAdd(){
double result = value1 + value2;
assertTrue(result == 6);
}
}
1.2 Unit tests and unit testing
A unit test is a piece of code written by a developer that executes a specific functionality in the code to be tested and asserts a certain behavior or state.
The percentage of code which is tested by unit tests is typically called Test coverage.
1.3 Integration tests
An integration test aims to test the behavior of a component or the integration between a set of components. The term functional test is sometimes used as synonym for integration test. Integration tests check that the whole system works as intended, therefore they are reducing the need for intensive manual tests.
These kinds of tests allow you to translate your user stories into a test suite. The test would resemble an expected user interaction with application.
Integration testing (sometimes called integration and testing, abbreviated I&T) is the phase in software testing in which individual software modules are combined and tested as a group. It occurs after unit testing and before validation testing.
1.3.1 Validation testing
In software project management, software testing, and software engineering, verification and validation(V&V) is the process of checking that a software system meets specifications and that it fulfills its intended purpose. It may also be referred to as software quality control. It is normally the responsibility of software testers as part of the software development lifecycle. In simple terms, software verification is: "Assuming we should build X, does our software achieve its goals without any bugs or gaps?" On the other hand, software validation is: "Was X what we should have built? Does X meet the high level requirements?"
1.4 Performance tests
Performance tests are used to benchmark software components repeatedly. Their purpose is to ensure that the code under test runs fast enough even if it's under high load.
1.5 Behavior vs. state testing
A test is a behavior test (also called interaction test) if it checks if certain methods were called with the correct input parameters. A behavior test does not validate the result of a method call.
State testing is about validating the result. Behavior testing is about testing the behavior of the application under test.
If you are testing algorithms or system functionality, in most cases you may want to test state and not interactions. A typical test setup uses mocks or stubs of related classes to abstract the interactions with these other classes away Afterwards you test the state or the behavior depending on your need.
1.6 Where should the test be located?
src/main/java - for Java classes
src/test/java - for test classes
2. JUnit
2.1 Annotations for Junit testing
The Junit 4.x framework is annotation based, so let's see the annotations that can be used while writing the test cases.
@Test annotation specifies that method is the test method
@Test(timeout = 10000) annotation specifies that method will be failed if it takes longer than 10000 milliseconds(10 seconds)
@BeforeClass annotation specifies that method will be invoked only once, before starting all the tests.
@Before annotation specifies that method will be invoked before each test.
@After annotation specifies that method will be invoked after each test.
@AfterClass annotation specifies that method will be invoked only once, after finishing all the test.
2.2 Assert Class
2.2.1 Methods of Assert class
void assertEquals(boolean expected, boolean actual): checks that two primitives/objects are equal. It is overload.
void assertTrue(boolean condition): checks that a condition is true.
void assertFalse(boolean condition): checks that a condition is false.
void assertNull(Object obj): checks that object is null.
void assertNotNull(Object obj): checks that object is not null.
2.3 TestSuite Class
A TestSuite is a Composite of tests. It runs a collection of test cases.
e.g.
import junit.framework.*;
public class JunitTestSuite {
public static void main(String[] a) {
// add the test's in the suite
TestSuite suite = new TestSuite(TestJunit1.class, TestJunit2.class, TestJunit3.class );
TestResult result = new TestResult();
suite.run(result);
System.out.println("Number of test cases = " + result.runCount());
}
}
e.g.2
import org.junit.runner.RunWith;
import org.junit.runners.Suite;
import org.junit.runners.Suite.SuiteClasses;
@RunWith(Suite.class)
@SuiteClass({
MyClassTest.class,
MySeconeClassTest.class})
public class AllTest{
}
2.4 Parameterized test
JUnit allows you to use parameters in a tests class. This class can contain one test method and this method is executed with the different parameters provided.
You mark a test class as a parameterized test with the @RunWith(Parameterized.class)
annotation.
Such a test class must contain a static method annotated with the @Parameters annotation. That method generates and returns a collection of arrays. Each item in this collection is used as parameter for the test method.
You can use the @Parameter annotation on public fields to get the test values injected in the test.
e.g.
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;
import java.util.Arrays;
import java.util.Collection;
import static org.junit.Assert.assertEquals;
import static org.junit.runners.Parameterized.*;
@RunWith(Parameterized.class)
public class ParameterizedTestFields {
// fields used together with @Parameter must be public
@Parameter(0)
public int m1;
@Parameter(1)
public int m2;
@Parameter(2)
public int result;
// creates the test data
@Parameters
public static Collection<Object[]> data() {
Object[][] data = new Object[][] { { 1 , 2, 2 }, { 5, 3, 15 }, { 121, 4, 484 } };
return Arrays.asList(data);
}
@Test
public void testMultiplyException() {
MyClass tester = new MyClass();
assertEquals("Result", result, tester.multiply(m1, m2));
}
// class to be tested
class MyClass {
public int multiply(int i, int j) {
return i *j;
}
}
}
Alternatively to using the@Parameter
annotation you can use a constructor in which you store the values for each test. The number of elements in each array provided by the method annotated with @Parameters
must correspond to the number of parameters in the constructor of the class. The class is created for each parameter and the test values are passed via the constructor to the class.
e.g.
import static org.junit.Assert.assertEquals;
import java.util.Arrays;
import java.util.Collection;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;
@RunWith(Parameterized.class)
public class ParameterizedTestUsingConstructor {
private int m1;
private int m2;
public ParameterizedTestUsingConstructor(int p1, int p2) {
m1 = p1;
m2 = p2;
}
// creates the test data
@Parameters
public static Collection<Object[]> data() {
Object[][] data = new Object[][] { { 1 , 2 }, { 5, 3 }, { 121, 4 } };
return Arrays.asList(data);
}
@Test
public void testMultiplyException() {
MyClass tester = new MyClass();
assertEquals("Result", m1 * m2, tester.multiply(m1, m2));
}
// class to be tested
class MyClass {
public int multiply(int i, int j) {
return i *j;
}
}
}
3. Mockito API
3.1 mocking
mocking is creating objects that simulate the behavior of real objects. Unlike stubs, mocks can be dynamically created from code - at runtime. Mocks offer more functionality than stubbing. You can verity method calls and a lot of other things.
when-thenReturn
e.g.
TodoService todoServiceMock = mock(TodoService.class);
List<String> todos = Arrays.asList("Learn Spring MVC","Learn Spring", "Learn to Dance");
when(todoServiceMock.retrieveTodos("Dummy")).thenReturn(todos);
3.2 stub
need to create a real class to do the work.
3.3 BDD
BDD mockito methods are very similar to usual mockito methods. It is Behaviour Driven Development
Given-When-Then
given-willReturn
e.g.
@Test
public void mockListGet_usingBDD() {
//Given
List<String> listMock = mock(List.class);
given(listMock.get(anyInt())).willReturn("in");
//when
String firstElement = listMock.get(0);
//Then
assertThat(firstElement, is("in"));
//assertEquals(null, listMock.get(1));
}
Last updated
Was this helpful?