forked from matyb/java-koans
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAboutMocks.java
More file actions
executable file
·42 lines (35 loc) · 1023 Bytes
/
AboutMocks.java
File metadata and controls
executable file
·42 lines (35 loc) · 1023 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
package advanced;
import com.sandwich.koan.Koan;
import static com.sandwich.util.Assert.fail;
public class AboutMocks {
static interface Collaborator {
public void doBusinessStuff();
}
static class ExplosiveCollaborator implements Collaborator {
public void doBusinessStuff() {
fail("Default collaborator's behavior is complicating testing.");
}
}
static class ClassUnderTest {
Collaborator c;
public ClassUnderTest(){
// default is to pass a broken Collaborator, test should pass one
// that doesn't throw exception
this(new ExplosiveCollaborator());
}
public ClassUnderTest(Collaborator c){
this.c = c;
}
public boolean doSomething(){
c.doBusinessStuff();
return true;
}
}
@Koan
public void simpleAnonymousMock(){
// HINT: pass a safe Collaborator implementation to constructor
// new ClassUnderTest(new Collaborator(){... it should not be the
// objective of this test to test that collaborator, so replace it
new ClassUnderTest().doSomething();
}
}