public class GettingStartedJUnit3Mockomatic extends MockObjectTestCase { @Mock Subscriber subscriber; public void testOneSubscriberReceivesAMessage() { // set up Publisher publisher = new Publisher(); publisher.add(subscriber); final String message = "message"; // expectations checking(new Expectations() {{ oneOf(subscriber).receive(message); }}); // execute publisher.publish(message); } }
public static class WantsStates { public @Auto States stateMachine; }
public static class WantsSequence { public @Auto Sequence aSequence; }
public static class BaseClass { public @Mock MockedType mockInBaseClass; } public static class DerivedClass extends BaseClass {
public static class DerivedClass extends BaseClass { public @Mock MockedType mockInDerivedClass; }
public class GettingStartedJUnit4Mockomatic { @Rule public JUnitRuleMockery context = new JUnitRuleMockery(); @Mock Subscriber subscriber; @Test public void oneSubscriberReceivesAMessage() { // set up Publisher publisher = new Publisher(); publisher.add(subscriber); final String message = "message"; // expectations context.checking(new Expectations() {{ oneOf(subscriber).receive(message); }}); // execute publisher.publish(message); } }
public class GettingStartedJUnit4RuleMockomatic { @Rule public JUnitRuleMockery context = new JUnitRuleMockery(); @Mock Subscriber subscriber; @Test public void oneSubscriberReceivesAMessage() { // set up Publisher publisher = new Publisher(); publisher.add(subscriber); final String message = "message"; // expectations context.checking(new Expectations() {{ oneOf(subscriber).receive(message); }}); // execute publisher.publish(message); } }
public static class Plural extends DomainObjectLayoutFactoryTest { @Mock ObjectAdapter mockAdapter;
public static class Named extends DomainObjectLayoutFactoryTest { @Mock ObjectAdapter mockAdapter;
public static class Paged extends DomainObjectLayoutFactoryTest { @Mock ObjectAdapter mockAdapter;
public class PrototypeFacetAbstractTest { @Rule public JUnitRuleMockery2 context = JUnitRuleMockery2.createFor(JUnitRuleMockery2.Mode.INTERFACES_AND_CLASSES); @Mock private VisibilityContext mockVisibilityContext; @Mock private FacetHolder mockFacetHolder; @Test public void allCombinations() throws Exception { givenWhenThen(DeploymentCategory.EXPLORING, null); givenWhenThen(DeploymentCategory.PROTOTYPING, null); givenWhenThen(DeploymentCategory.PRODUCTION, "Prototyping action not visible in production mode"); } protected void givenWhenThen(final DeploymentCategory deploymentCategory, final String expected) { // given final PrototypeFacetAbstract facet = new PrototypeFacetAbstract(mockFacetHolder, deploymentCategory){}; // when final String reason = facet.hides(mockVisibilityContext); // then assertEquals(expected, reason); } }
public class JUnitRuleMockery2Test_autoWiring_sadCase_noClassUnderTest { @Rule public JUnitRuleMockery2 context = JUnitRuleMockery2.createFor(Mode.INTERFACES_AND_CLASSES); @Mock private Collaborator collaborator; // @ClassUnderTest @SuppressWarnings("unused") private CollaboratingUsingConstructorInjection collaborating; @Test(expected=IllegalStateException.class) public void cannotFindClassUnderTest() { context.getClassUnderTest(); } }
public class JUnitRuleMockery2Test { @Rule public JUnitRuleMockery2 context = JUnitRuleMockery2.createFor(Mode.INTERFACES_AND_CLASSES); @Mock private Collaborator collaborator; @Test public void poke() { context.checking(new Expectations() { { one(collaborator).doOtherStuff(); } }); new CollaboratingUsingConstructorInjection(collaborator).collaborateWithCollaborator(); } }
public final JUnitRuleMockery2 context = JUnitRuleMockery2.createFor(Mode.INTERFACES_AND_CLASSES); @Mock private Request stubRequest;
public final JUnitRuleMockery2 context = JUnitRuleMockery2.createFor(Mode.INTERFACES_AND_CLASSES); @Mock private Request stubRequest;
public class JUnitRuleMockery2Test_autoWiring_setterInjection_happyCase { @Rule public JUnitRuleMockery2 context = JUnitRuleMockery2.createFor(Mode.INTERFACES_AND_CLASSES); @Mock private Collaborator collaborator; @ClassUnderTest private CollaboratingUsingSetterInjection collaborating; // no longer necessary :-) // @Before // public void setUp() throws Exception { // collaborating = context.getClassUnderTest(); // } @Test public void wiring() { assertThat(collaborating.collaborator, is(not(nullValue()))); } }
public class JUnitRuleMockery2Test_autoWiring_constructorInjection_happyCase { @Rule public JUnitRuleMockery2 context = JUnitRuleMockery2.createFor(Mode.INTERFACES_AND_CLASSES); @Mock private Collaborator collaborator; @ClassUnderTest private CollaboratingUsingConstructorInjection collaborating; // no longer necessary :-) // @Before // public void setUp() throws Exception { // collaborating = (CollaboratingUsingConstructorInjection) context.getClassUnderTest(); // } @Test public void wiring() { assertThat(collaborating.collaborator, is(not(nullValue()))); } }
public class JUnitRuleMockery2Test_autoWiring_sadCase_twoClassesUnderTest { @Rule public JUnitRuleMockery2 context = JUnitRuleMockery2.createFor(Mode.INTERFACES_AND_CLASSES); @Mock private Collaborator collaborator; @ClassUnderTest private CollaboratingUsingConstructorInjection collaborating; @ClassUnderTest private CollaboratingUsingConstructorInjection anotherCollaborating; @Ignore("This isn't actually possible to test, because the test is actually thrown by the rule, which is further up the callstack than the test method") @Test(expected=AssertionError.class) public void cannotFindClassUnderTest() { context.getClassUnderTest(); } }