Skip to content

Commit a2b8359

Browse files
committed
Merge pull request iluwatar#269 from iamrichardjones/master
Add unit test to callback method
2 parents 8eff279 + 21a4d7b commit a2b8359

File tree

2 files changed

+26
-6
lines changed

2 files changed

+26
-6
lines changed

callback/src/main/java/com/iluwatar/callback/SimpleTask.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ public class SimpleTask extends Task {
99

1010
@Override
1111
public void execute() {
12-
System.out.println("Perform some important activity.");
12+
System.out.println("Perform some important activity and after call the callback method.");
1313
}
1414

1515
}

callback/src/test/java/com/iluwatar/callback/AppTest.java

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,38 @@
22

33
import org.junit.Test;
44

5-
import com.iluwatar.callback.App;
5+
import static org.junit.Assert.assertEquals;
66

77
/**
8-
*
9-
* Application test
8+
* Add a field as a counter. Every time the callback method is called increment this
9+
* field. Unit test checks that the field is being incremented.
1010
*
11+
* Could be done with mock objects as well where the call method call is verified.
1112
*/
1213
public class AppTest {
1314

15+
private Integer callingCount = 0;
16+
1417
@Test
1518
public void test() {
16-
String[] args = {};
17-
App.main(args);
19+
Callback callback = new Callback() {
20+
@Override
21+
public void call() {
22+
callingCount++;
23+
}
24+
};
25+
26+
Task task = new SimpleTask();
27+
28+
assertEquals("Initial calling count of 0", new Integer(0), callingCount);
29+
30+
task.executeWith(callback);
31+
32+
assertEquals("Callback called once", new Integer(1), callingCount);
33+
34+
task.executeWith(callback);
35+
36+
assertEquals("Callback called twice", new Integer(2), callingCount);
37+
1838
}
1939
}

0 commit comments

Comments
 (0)