Merge pull request #269 from iamrichardjones/master
Add unit test to callback method
This commit is contained in:
commit
a2b8359ab5
@ -9,7 +9,7 @@ public class SimpleTask extends Task {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void execute() {
|
public void execute() {
|
||||||
System.out.println("Perform some important activity.");
|
System.out.println("Perform some important activity and after call the callback method.");
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -2,18 +2,38 @@ package com.iluwatar.callback;
|
|||||||
|
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
import com.iluwatar.callback.App;
|
import static org.junit.Assert.assertEquals;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
* Add a field as a counter. Every time the callback method is called increment this
|
||||||
|
* field. Unit test checks that the field is being incremented.
|
||||||
*
|
*
|
||||||
* Application test
|
* Could be done with mock objects as well where the call method call is verified.
|
||||||
*
|
|
||||||
*/
|
*/
|
||||||
public class AppTest {
|
public class AppTest {
|
||||||
|
|
||||||
|
private Integer callingCount = 0;
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void test() {
|
public void test() {
|
||||||
String[] args = {};
|
Callback callback = new Callback() {
|
||||||
App.main(args);
|
@Override
|
||||||
|
public void call() {
|
||||||
|
callingCount++;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
Task task = new SimpleTask();
|
||||||
|
|
||||||
|
assertEquals("Initial calling count of 0", new Integer(0), callingCount);
|
||||||
|
|
||||||
|
task.executeWith(callback);
|
||||||
|
|
||||||
|
assertEquals("Callback called once", new Integer(1), callingCount);
|
||||||
|
|
||||||
|
task.executeWith(callback);
|
||||||
|
|
||||||
|
assertEquals("Callback called twice", new Integer(2), callingCount);
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user