Coder Social home page Coder Social logo

mupezzuol / tdd-java Goto Github PK

View Code? Open in Web Editor NEW
0.0 1.0 0.0 89.12 MB

Design for automated test studies.

License: MIT License

Java 100.00%
tdd-java tdd java java-8 java8 testing test test-automation testing-tools test-driven-development test-framework junit junit-test junit5 junit4 mockito mock hamcrest hamcrest-matchers

tdd-java's Introduction

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

tdd-java's People

Contributors

mupezzuol avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.