@Test public void uploadFileToCloudStorage() { when(mockCloudService.write( WriteRequest.newBuilder().setUserId(“testuser”).setFileType(“plain/text”)...)) .thenReturn(WriteResponse.newBuilder().setUploadId(“uploadId”).build()); CloudUploader cloudUploader = new CloudUploader(mockCloudService); Uri uri = cloudUploader.uploadFile(new File(“/path/to/foo.txt”)); // The uploaded file URI contains the user ID, file type, and upload ID. (Or does it?) assertThat(uri).isEqualTo(new Uri(“/testuser/text/uploadId.txt”));
@Test public void uploadFileToCloudStorage() { CloudUploader cloudUploader = new CloudUploader(cloudService); Uri uri = cloudUploader.uploadFile(”/path/to/foo.txt”); assertThat(cloudService.retrieveFile(uri)).isEqualTo(readContent(“/path/to/foo.txt")); }
TEST_F(BankAccountTest, WithdrawFromAccount) { Transaction transaction = account_.Deposit(Usd(5)); clock_.AdvanceTime(MIN_TIME_TO_SETTLE); account_.Settle(transaction); EXPECT_THAT(account_.Withdraw(Usd(5)), IsOk()); EXPECT_THAT(account_.Withdraw(Usd(1)), IsRejected()); account_.SetOverdraftLimit(Usd(1)); EXPECT_THAT(account_.Withdraw(Usd(1)), IsOk()); }
TEST_F(BankAccountTest, CanWithdrawWithinBalance) { DepositAndSettle(Usd(5)); // Common setup code is extracted into a helper method. EXPECT_THAT(account_.Withdraw(Usd(5)), IsOk()); } TEST_F(BankAccountTest, CannotOverdraw) { DepositAndSettle(Usd(5)); EXPECT_THAT(account_.Withdraw(Usd(6)), IsRejected()); } TEST_F(BankAccountTest, CanOverdrawUpToOverdraftLimit) { DepositAndSettle(Usd(5)); account_.SetOverdraftLimit(Usd(1)); EXPECT_THAT(account_.Withdraw(Usd(6)), IsOk()); }
// This helper method starts with just a single parameter: Company company = newCompany(PUBLIC);
// But soon it acquires more and more parameters. // Conditionals creep into the newCompany() method body to handle the nulls, // and the method calls become hard to read due to the long parameter lists: Company small = newCompany(2, 2, null, PUBLIC); Company privatelyOwned = newCompany(null, null, null, PRIVATE); Company bankrupt = newCompany(null, null, PAST_DATE, PUBLIC); // Or a new method is added each time a test needs a different combination of fields: Company small = newCompanyWithEmployeesAndBoardMembers(2, 2, PUBLIC); Company privatelyOwned = newCompanyWithType(PRIVATE); Company bankrupt = newCompanyWithBankruptcyDate(PAST_DATE, PUBLIC);
Company small = newCompany().setEmployees(2).setBoardMembers(2).build(); Company privatelyOwned = newCompany().setType(PRIVATE).build(); Company bankrupt = newCompany().setBankruptcyDate(PAST_DATE).build(); Company arbitraryCompany = newCompany().build();
// Zero parameters makes this method reusable for different variations of Company. // It also doesn’t need conditionals to ignore parameters that aren’t set (e.g. null // values) since a test can simply not set a field if it doesn’t care about it. private static Company.Builder newCompany() { return Company.newBuilder().setType(PUBLIC).setEmployees(100); // Set required fields }
// This test needs a public company, so explicitly set it. // It also needs a company with no board members, so explicitly clear it. Company publicNoBoardMembers = newCompany().setType(PUBLIC).clearBoardMembers().build();
208: @Test public void testIncrement_existingKey() { 209: assertEquals(9, tally.get("key1")); 210: }
1: private final Tally tally = new Tally(); 2: @Before public void setUp() { 3: tally.increment("key1", 8); 4: tally.increment("key2", 100); 5: tally.increment("key1", 0); 6: tally.increment("key1", 1); 7: } // 200 lines away 208: @Test public void testIncrement_existingKey() { 209: assertEquals(9, tally.get("key1")); 210: }
1: private final Tally tally = new Tally(); 2: @Test public void testIncrement_newKey() { 3: tally.increment("key", 100); 5: assertEquals(100, tally.get("key")); 6: } 7: @Test public void testIncrement_existingKey() { 8: tally.increment("key", 8); 9: tally.increment("key", 1); 10: assertEquals(9, tally.get("key")); 11: } 12: @Test public void testIncrement_incrementByZeroDoesNothing() { 13: tally.increment("key", 8); 14: tally.increment("key", 0); 15: assertEquals(8, tally.get("key")); 16: }