@Test public void all_valid_exits_are_retrieveable() { final Exit exit1 = mockery.mock(Exit.class, "exit1"); final Exit exit2 = mockery.mock(Exit.class, "exit2"); mockery.checking( new Expectations() { { allowing(exit1).visible(); will(returnValue(true)); ignoring(exit1); allowing(exit2).visible(); will(returnValue(true)); ignoring(exit2); } }); List<Exit> exits = new ArrayList<Exit>(); exits.add(exit1); exits.add(exit2); Location l = createLocation(); l.addExit(exit1); l.addExit(exit2); assertEquals(exits, l.visibleExits()); }
@Test public void available_items_text_includes_only_visible_items() { final Item visibleItem = mockery.mock(Item.class, "visible item"); final Item invisibleItem = mockery.mock(Item.class, "invisible item"); mockery.checking( new Expectations() { { allowing(visibleItem).visible(); will(returnValue(true)); allowing(visibleItem).countableNounPrefix(); will(returnValue("a")); allowing(visibleItem).midSentenceCasedName(); will(returnValue("visible item")); ignoring(visibleItem); allowing(invisibleItem).visible(); will(returnValue(false)); allowing(invisibleItem).midSentenceCasedName(); will(returnValue("invisible item")); ignoring(invisibleItem); } }); Location l = new Location("", "Location description.", null, null); l.addItem(visibleItem); l.addItem(invisibleItem); assertEquals("There is a visible item here.\n", l.availableItemsText()); }
@Test public void actions_uses_action_factory_to_create_ExamineAnItem_action_for_all_visible_items() { final Item visibleItem = mockery.mock(Item.class, "visible item"); final Item invisibleItem = mockery.mock(Item.class, "invisible item"); final ActionFactory actionFactory = mockery.mock(ActionFactory.class); final Action examineAnItemAction = mockery.mock(Action.class); final List<Item> visibleItems = new ArrayList<Item>(); visibleItems.add(visibleItem); mockery.checking( new Expectations() { { allowing(visibleItem).visible(); will(returnValue(true)); ignoring(visibleItem); allowing(invisibleItem).visible(); will(returnValue(false)); ignoring(invisibleItem); oneOf(actionFactory).createExamineAnItemAction(with(equal(visibleItems))); will(returnValue(examineAnItemAction)); ignoring(actionFactory); } }); Location l = new Location("", "", null, actionFactory); l.addItem(visibleItem); l.addItem(invisibleItem); List<Action> actions = l.actions(); assertTrue(actions.size() > 0); assertThat(actions.get(0), is(examineAnItemAction)); }
@Test public void pickFruits() { final FruitTree mangoTree = context.mock(FruitTree.class); final Mango mango1 = new Mango(); final Mango mango2 = new Mango(); context.checking( new Expectations() { { oneOf(mangoTree).pickFruit(with(any(Collection.class))); will( new VoidAction() { public Object invoke(Invocation invocation) { @SuppressWarnings({"unchecked"}) Collection<Fruit> fruits = (Collection<Fruit>) invocation.getParameter(0); fruits.add(mango1); fruits.add(mango2); return null; } }); } }); Collection<Fruit> fruits = new FruitPicker().pickFruits(asList(mangoTree)); assertEquals(asList(mango1, mango2), fruits); }
@Test public void removing_all_items_from_a_location_removes_TakeAnItem_action_from_action_list() { final Item takeableItem = mockery.mock(Item.class); final ActionFactory actionFactory = mockery.mock(ActionFactory.class); final Location l = new Location("", "", null, actionFactory); mockery.checking( new Expectations() { { allowing(takeableItem).visible(); will(returnValue(true)); allowing(takeableItem).takeable(); will(returnValue(true)); ignoring(takeableItem); never(actionFactory) .createTakeAnItemAction( with(any(List.class)), with(any(UserInventory.class)), with(any(ModelLocation.class))); ignoring(actionFactory); } }); l.addItem(takeableItem); l.removeItem(takeableItem); l.actions(); }
@Test public void exits_that_havent_been_added_are_not_exitable() { final Exit exit = mockery.mock(Exit.class); mockery.checking( new Expectations() { { ignoring(exit); } }); Location l = createLocation(); assertFalse(l.exitable(exit)); }
@RunWith(JMock.class) public class FormatTransactionAsCsvRowTest { private final Mockery mockery = new Mockery(); private final CsvFormat<LocalDate> dateFormat = mockery.mock(CsvFormat.class, "date format"); private final CsvFormat<Category> categoryFormat = mockery.mock(CsvFormat.class, "category format"); private final CsvFormat<Amount> amountFormat = mockery.mock(CsvFormat.class, "amount format"); private final TransactionCsvFormat transactionCsvFormat = new TransactionCsvFormat(dateFormat, categoryFormat, amountFormat); private LocalDate anyNonNullDate = new LocalDate(2012, 11, 14); private Category anyNonNullCategory = new Category("Bowling Winnings"); private Amount anyNonNullAmount = Amount.cents(250); @Test public void happyPath() throws Exception { mockery.checking( new Expectations() { { allowing(dateFormat).format(with(any(LocalDate.class))); will(returnValue("::the date::")); allowing(categoryFormat).format(with(any(Category.class))); will(returnValue("::the category::")); allowing(amountFormat).format(with(any(Amount.class))); will(returnValue("::the amount::")); } }); final Transaction transaction = new Transaction(anyNonNullDate, anyNonNullCategory, anyNonNullAmount); final String rowText = transactionCsvFormat.format(transaction); assertThat( rowText, matches( Pattern.compile( "\\s*\"::the date::\"," + "\\s*\"::the category::\"," + "\\s*\"::the amount::\"\\s*"))); } @Test public void nullTransaction() throws Exception { try { transactionCsvFormat.format(null); fail("How did you format a null transaction?!"); } catch (ProgrammerMistake success) { } } }
@Test public void exit_destination_is_retrievable() { final Exit exit = mockery.mock(Exit.class); mockery.checking( new Expectations() { { allowing(exit).destination(); will(returnValue("destination")); ignoring(exit); } }); Location l = createLocation(); assertEquals("destination", l.exitDestinationFor(exit)); }
@Test public void added_exit_makes_the_exit_exitable() { final Exit exit = mockery.mock(Exit.class); mockery.checking( new Expectations() { { allowing(exit).visible(); will(returnValue(true)); ignoring(exit); } }); Location l = createLocation(); l.addExit(exit); assertTrue(l.exitable(exit)); }
@Test public void non_visible_exits_are_not_exitable() { final Exit exit = mockery.mock(Exit.class); mockery.checking( new Expectations() { { allowing(exit).visible(); will(returnValue(false)); ignoring(exit); } }); Location l = createLocation(); l.addExit(exit); assertFalse(l.exitable(exit)); }
@Test public void label_is_target_item_name() { final Item item = mockery.mock(Item.class); mockery.checking( new Expectations() { { allowing(item).name(); will(returnValue("Item name")); ignoring(item); } }); UseWithSpecificItem action = new UseWithSpecificItem(null, item); assertEquals("Item name", action.label()); }
@Test public void non_visible_exits_are_not_in_the_exits_list() { final Exit exit = mockery.mock(Exit.class); mockery.checking( new Expectations() { { allowing(exit).visible(); will(returnValue(false)); ignoring(exit); } }); Location l = createLocation(); l.addExit(exit); assertEquals(0, l.visibleExits().size()); }
@Test public void passes_UserInventory_to_created_object() { final UserInventory inventory = mockery.mock(UserInventory.class); Location l = (Location) new LocationFactory(inventory, null).create(); assertEquals(inventory, l.inventory()); }
@Test public void decrement_Execute() { // Arrange mockery.checking( new Expectations() { { oneOf(executionContext).decrement(); } }); // Act Instruction.DECREMENT.execute(executionContext); // Assert mockery.assertIsSatisfied(); }
@Test public void testAndJumpBackward_Execute() { // Arrange mockery.checking( new Expectations() { { oneOf(executionContext).testAndJumpBackward(); } }); // Act Instruction.TEST_AND_JUMP_BACKWARD.execute(executionContext); // Assert mockery.assertIsSatisfied(); }
@Test public void happyPath() throws Exception { mockery.checking( new Expectations() { { allowing(dateFormat).format(with(any(LocalDate.class))); will(returnValue("::the date::")); allowing(categoryFormat).format(with(any(Category.class))); will(returnValue("::the category::")); allowing(amountFormat).format(with(any(Amount.class))); will(returnValue("::the amount::")); } }); final Transaction transaction = new Transaction(anyNonNullDate, anyNonNullCategory, anyNonNullAmount); final String rowText = transactionCsvFormat.format(transaction); assertThat( rowText, matches( Pattern.compile( "\\s*\"::the date::\"," + "\\s*\"::the category::\"," + "\\s*\"::the amount::\"\\s*"))); }
@Test public void output_Execute() { // Arrange mockery.checking( new Expectations() { { oneOf(executionContext).ouput(); } }); // Act Instruction.OUTPUT.execute(executionContext); // Assert mockery.assertIsSatisfied(); }
@Test public void a_location_without_items_does_not_need_a_TakeAnItem_action() { final ActionFactory actionFactory = mockery.mock(ActionFactory.class); final Location l = new Location("", "", null, actionFactory); mockery.checking( new Expectations() { { never(actionFactory) .createTakeAnItemAction( with(any(List.class)), with(any(UserInventory.class)), with(any(ModelLocation.class))); ignoring(actionFactory); } }); l.actions(); }
@Test public void moveBackward_Execute() { // Arrange mockery.checking( new Expectations() { { oneOf(executionContext).moveBackward(); } }); // Act Instruction.MOVE_BACKWARD.execute(executionContext); // Assert mockery.assertIsSatisfied(); }
@Test public void using_an_item_causes_the_target_item_to_be_used() { final Item original = mockery.mock(Item.class, "original"); final Item target = mockery.mock(Item.class, "target"); mockery.checking( new Expectations() { { allowing(original).id(); will(returnValue("originalid")); ignoring(original); oneOf(target).useWith(original); ignoring(target); } }); UseWithSpecificItem action = new UseWithSpecificItem(original, target); action.trigger(); }
@Test public void noOperation_Execute() { // Arrange // Act Instruction.NO_OPERATION.execute(executionContext); // Assert mockery.assertIsSatisfied(); }
@Test public void willLimitBidToMaximum() throws Exception { context.checking( new Expectations() { { exactly(1).of(auction).bid(maximumBid); } }); sniper.bidAccepted(maximumBid.subtract(new Money(1))); }
@Test public void willAnnounceItHasFinishedIfPriceGoesAboveMaximum() { context.checking( new Expectations() { { exactly(1).of(listener).sniperFinished(sniper); } }); sniper.bidAccepted(unbeatableBid); }
@Test public void willNotBidPriceGreaterThanMaximum() throws Exception { context.checking( new Expectations() { { ignoring(listener); never(auction).bid(with(any(Money.class))); } }); sniper.bidAccepted(unbeatableBid); }
@Test public void using_an_item_changes_the_user_text_to_the_use_with_message_of_the_target_item() { final Item original = mockery.mock(Item.class, "original"); final Item target = mockery.mock(Item.class, "target"); mockery.checking( new Expectations() { { ignoring(original); allowing(target).canBeUsedWith(original); will(returnValue(true)); allowing(target).useWith(original); will(returnValue("use with text")); ignoring(target); } }); UseWithSpecificItem action = new UseWithSpecificItem(original, target); action.trigger(); assertThat(action.userText(), stringEndsWith("use with text")); }
@Test public void triesToBeatTheLatestHighestBid() throws Exception { final Money expectedBid = beatableBid.add(increment); context.checking( new Expectations() { { oneOf(auction).bid(expectedBid); } }); sniper.bidAccepted(beatableBid); }
@Test public void location_action_to_take_an_item_is_not_included_if_only_untakeable_items_available() { final Item untakeableItem = mockery.mock(Item.class, "untakeable item"); final ActionFactory actionFactory = mockery.mock(ActionFactory.class); final Location l = new Location("", "", null, actionFactory); mockery.checking( new Expectations() { { allowing(untakeableItem).visible(); will(returnValue(true)); allowing(untakeableItem).takeable(); will(returnValue(false)); ignoring(untakeableItem); never(actionFactory) .createTakeAnItemAction( with(any(List.class)), with(any(UserInventory.class)), with(any(ModelLocation.class))); ignoring(actionFactory); } }); l.addItem(untakeableItem); l.actions(); }
@Test public void catchesExceptionsAndReportsThemToErrorListener() throws Exception { final AuctionException exception = new AuctionException("test"); context.checking( new Expectations() { { allowing(auction).bid(with(any(Money.class))); will(throwException(exception)); exactly(1).of(listener).sniperBidFailed(sniper, exception); } }); sniper.bidAccepted(beatableBid); }
@Test public void modelBlowsUpInAnUnavoidableWay() throws Exception { mockery.checking( new Expectations() { { allowing(browseTransactionsModel).findAllTransactions(); will(throwException(new InternalStorageException())); } }); pressExportAllTransactionsButton(browseTransactionsActivity); assertLastToastMatchesRegex( "Something strange just happened. Try again. You might need to reinstall the " + "application. I feel embarrassed and ashamed."); }
@Test public void exportWhateverTheModelProvides() throws Exception { final List<Transaction> anyNonEmptyListOfTransactions = ObjectMother.anyNonEmptyListOfTransactions(); mockery.checking( new Expectations() { { allowing(browseTransactionsModel).findAllTransactions(); will(returnValue(anyNonEmptyListOfTransactions)); ignoring(androidDevicePublicStorageGateway); oneOf(exportAllTransactionsAction).execute(with(anyNonEmptyListOfTransactions)); } }); pressExportAllTransactionsButton(browseTransactionsActivity); }