11package advanced ;
22
33import com .sandwich .koan .Koan ;
4+ import static com .sandwich .util .Assert .fail ;
45
56public class AboutMocks {
67
78 @ Koan ()
89 public void simpleAnonymousMock (){
9- // HINT: pass different Collaborator implementation to constructor
10- // new ClassUnderTest(new Colloborator(){...
10+ // HINT: pass a safe Collaborator implementation to constructor
11+ // new ClassUnderTest(new Colloborator(){... it should not be the
12+ // objective of this test to test that collaborator, so replace it
1113 new ClassUnderTest ().doSomething ();
12- // TODO: ponder why this assertion was failing
13- // originally... look in default constructor
1414 }
1515
1616 static interface Collaborator {
1717 public void doBusinessStuff ();
1818 }
1919
20+ static class ExplosiveCollaborator implements Collaborator {
21+ public void doBusinessStuff () {
22+ fail ("Default collaborator's behavior is complicating testing." );
23+ }
24+ }
25+
2026 static class ClassUnderTest {
2127 Collaborator c ;
2228 public ClassUnderTest (){
2329 // default is to pass a broken Collaborator, test should pass one
2430 // that doesn't throw exception
25- this (new Collaborator (){
26- public void doBusinessStuff () {
27- throw new AssertionError ("Default collaborator's behavior is complicating testing." );
28- }
29- });
31+ this (new ExplosiveCollaborator ());
3032 }
3133 public ClassUnderTest (Collaborator c ){
3234 this .c = c ;
@@ -36,8 +38,5 @@ public boolean doSomething(){
3638 return true ;
3739 }
3840 }
39-
40-
41- //TODO: perhaps show off some mocking frameworks?
4241
4342}
0 commit comments