Skip to content

Instantly share code, notes, and snippets.

@koalahamlet
Created July 21, 2014 20:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save koalahamlet/75631c9c5f252643fcc1 to your computer and use it in GitHub Desktop.
Save koalahamlet/75631c9c5f252643fcc1 to your computer and use it in GitHub Desktop.
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(){
new ClassUnderTest().doSomething();
}
}
@koalahamlet
Copy link
Author

earlier I had tried

    public void simpleAnonymousMock(){
           new ClassUnderTest(
                    new Collaborator(){

            @Override
            public void doBusinessStuff() {
                //These are not the droids you're looking for
            }
        });
    new ClassUnderTest().doSomething();
}

but it still failed

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment