-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAboutMocks.java
More file actions
54 lines (42 loc) · 1.42 KB
/
AboutMocks.java
File metadata and controls
54 lines (42 loc) · 1.42 KB
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
43
44
45
46
47
48
49
50
51
52
53
54
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());
// c.doBusinessStuff();
}
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
Collaborator cd=new Collaborator() {
@java.lang.Override
public void doBusinessStuff() {
}
};
// AboutMocks.ExplosiveCollaborator d= new ExplosiveCollaborator();
new ClassUnderTest(cd).doSomething();
}
}