Skip to content

AgiliXRU/legacy-code-java

Repository files navigation

Repository for legacy code meetup

A-TDD

Read More

Working with legacy code


Legend: You have legacy (without tests) service which calculates order amount. You've hired separate testing team. They worked hard some months and wrote acceptance tests.


Discovering

  1. Go to legacy-code branch.
  2. Observe src/main/java/ru/agilix/workshop/bddandtdd/DeliveryContoller and src/test/resources/deliver.feature.
  3. Run src/main/java/ru/agilix/workshop/bddandtdd/Application.
  4. Open in a browser http://localhost:8080.
  5. Run src/test/resources/deliver.feature.

Go down with test

class DeliveryControllerShould {

    @ParameterizedTest
    @CsvSource({
            "Обычный, 999, 1249",
            "Обычный, 1000, 1000",
            "VIP, 999, 999",
            "VIP, 2499, 2499",
            "VIP, 2500, 2375"
    })
    void calculateDeliveryFee(String clientType, Integer cartAmount, Integer orderAmount) {
        assertEquals(orderAmount, new DeliveryController().calculate(clientType, cartAmount));
    }
}

Refactoring

  1. Add SpringTest:
    @SpringBootTest(webEnvironment= WebEnvironment.RANDOM_PORT)
    public class DeliveryServiceApprovalTest {
    
        @Autowired
        DeliveryController deliveryController;
    
        @Test
        void vipClientShouldNotPayForDelivery() {
            assertEquals(Integer.valueOf(2499), deliveryController.calculate("VIP", 2499));
        }
    }
  1. Add TDD DeliveryServiceShould and create DeliverService.
    @Test
    void addDeliveryFeeToOrderAmount() {
        assertEquals(1249, deliveryService.calculate(999));
    }

    @Test
    void NotAddDeliveryFeeToOrderAmountIfItIsEqualOrMoreThen1000() {
        assertEquals(1000, deliveryService.calculate(1000));
    }
  1. Replace call in DeliveryContoller
  2. Extract interface of DeliverService

About

Repostiroy for legacy code meetup

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors