001package advanced; 002 003import com.sandwich.koan.Koan; 004 005import static com.sandwich.util.Assert.fail; 006 007public class AboutMocks { 008 009 static interface Collaborator { 010 public void doBusinessStuff(); 011 } 012 013 static class ExplosiveCollaborator implements Collaborator { 014 public void doBusinessStuff() { 015 fail("Default collaborator's behavior is complicating testing."); 016 } 017 } 018 019 static class ClassUnderTest { 020 Collaborator c; 021 022 public ClassUnderTest() { 023 // default is to pass a broken Collaborator, test should pass one 024 // that doesn't throw exception 025 this(new ExplosiveCollaborator()); 026 } 027 028 public ClassUnderTest(Collaborator c) { 029 this.c = c; 030 } 031 032 public boolean doSomething() { 033 c.doBusinessStuff(); 034 return true; 035 } 036 } 037 038 @Koan 039 public void simpleAnonymousMock() { 040 // HINT: pass a safe Collaborator implementation to constructor 041 // new ClassUnderTest(new Collaborator(){... it should not be the 042 // objective of this test to test that collaborator, so replace it 043 new ClassUnderTest().doSomething(); 044 } 045 046}