Skip to content

mupezzuol/tdd-java

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

23 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

TDD with Java

Design for automated test studies.

Index 📌

Examples

  • First Test.
@Test
public void mustUnderstandLancesOrderAsc() {
    // 1. Scenario
    User userMurillo = new User("Murillo");
    User userGabriella = new User("Gabriella");
    User userRaul = new User("Raul");
	
    Auction auction = new Auction("Playstation 4");
	
    auction.propose(new Lance(userMurillo, 110.0));
    auction.propose(new Lance(userGabriella, 431.45));
    auction.propose(new Lance(userRaul, 550.0));
	
    // 2. Action
    Appraiser auctioneer = new Appraiser();
    auctioneer.evaluate(auction);
	
    // 3. Validation
    double higherExpected = 550.0;
    double lowerExpected = 110.0;
	
    // 4. Execution
    assertEquals(higherExpected, auctioneer.getHighestLance(), 0.00001);
    assertEquals(lowerExpected, auctioneer.getLowerLance(), 0.00001);
}
  • First Test with refactoring.
@Test
public void mustUnderstandLancesOrderAsc() {
    Auction auction = new AuctionBuilder().to("Macbook Pro 5")
            .lance(murilloPezzuol, 110.00)
            .lance(murilloPezzuol, 431.45)
            .lance(billGates, 550.00)
            .build();
    
    auctioneer.evaluate(auction);
    
    double higherExpected = 550.00;
    double lowerExpected = 110.00;
    
    assertEquals(higherExpected, auctioneer.getHighestLance(), 0.00001);
    assertEquals(lowerExpected, auctioneer.getLowerLance(), 0.00001);
}
  • Using @Before(jUnit) or @BeforeEach(jupiter)

before-junit

  • Testing list values
@Test
public void musFindTheBiggestLances() {
    Auction auction = new AuctionBuilder().to("PS5")
            .lance(murilloPezzuol, 110.0)
            .lance(stevenJobs, 3031.45)
            .lance(billGates, 550.0)
            .lance(murilloPezzuol, 1550.0)
            .lance(stevenJobs, 50.0)
            .build();
    
    auctioneer.evaluate(auction);
    
    double firstHighest = 3031.45;
    double secondHighest = 1550.0;
    double thirdHighest = 550.00;
    
    assertEquals(4, auctioneer.getHighestLances(4).size(), 0.00001);
    assertEquals(5, auctioneer.getHighestLances(10).size(), 0.00001);
    
    List<Lance> highestLances = auctioneer.getHighestLances(3);
    assertEquals(3, highestLances.size(), 0.00001);//Size List
    assertEquals(firstHighest, highestLances.get(0).getValue(), 0.00001);
    assertEquals(secondHighest, highestLances.get(1).getValue(), 0.00001);
    assertEquals(thirdHighest, highestLances.get(2).getValue(), 0.00001);
}
  • Handling exceptions
@Test // or use Expected with jUnit
public void shouldNotEvaluateAuctionWithoutLances() {
    Auction auction = new AuctionBuilder().to("Macbook Pro 5").build();
    Assertions.assertThrows(RuntimeException.class, () -> {
            auctioneer.evaluate(auction);
        });
}
  • Using Hamcrest
@Test
public void mustUnderstandLancesOrderAsc() {
    Auction auction = new AuctionBuilder().to("Macbook Pro 5")
            .lance(murilloPezzuol, 110.00)
            .lance(murilloPezzuol, 431.45)
            .lance(billGates, 550.00)
            .build();
    
    auctioneer.evaluate(auction);
    
    double higherExpected = 550.00;
    double lowerExpected = 110.00;
    
    // Using Hamcrest
    assertThat(auctioneer.getHighestLance(), equalTo(higherExpected));
    assertThat(auctioneer.getLowerLance(), equalTo(lowerExpected));
}
  • Using Hamcrest to validate lists
@Test
public void musFindTheBiggestLances() {
    Auction auction = new AuctionBuilder().to("PS5")
            .lance(murilloPezzuol, 110.0)
            .lance(stevenJobs, 3031.45)
            .lance(billGates, 550.0)
            .lance(murilloPezzuol, 1550.0)
            .lance(stevenJobs, 50.0)
            .build();
    
    auctioneer.evaluate(auction);
    
    assertEquals(4, auctioneer.getHighestLances(4).size(), 0.00001);
    assertEquals(5, auctioneer.getHighestLances(10).size(), 0.00001);
    
    List<Lance> highestLances = auctioneer.getHighestLances(3);
    
    assertEquals(3, highestLances.size(), 0.00001);//Size List
    
    // Using Hamcrest (hasItems use method 'hashcode' and 'equals')
    assertThat(highestLances, hasItems(
            new Lance(stevenJobs, 3031.45),
            new Lance(murilloPezzuol, 1550.0),
            new Lance(billGates, 550.0)
            ));
}

TDD 📈

  • Demonstrating the magic of TDD in a small gif of the project. tdd

Dependencies Used 🔗

  • JUnit Jupiter 5.6.2
  • Hamcrest 1.3