├── TestDrivenDevelopment ├── config.txt ├── src │ ├── main │ │ └── java │ │ │ └── lk │ │ │ └── himash │ │ │ ├── model │ │ │ └── City.java │ │ │ ├── controller │ │ │ └── CityController.java │ │ │ └── util │ │ │ └── CityDetails.java │ └── test │ │ └── java │ │ └── City.java └── pom.xml └── README.md /TestDrivenDevelopment/config.txt: -------------------------------------------------------------------------------- 1 | 01) TDD stands for Test Driven Development. 2 | 02) Use for test the business logic. 3 | 03) Advantages : 4 | 01) Code quality. 5 | 02) Code coverage. 6 | 03) TDD had own life-cycle name is RED GREEN REFACTOR. 7 | 01) RED - write test case for test case fail. 8 | 02) GREEN - pass the test case. 9 | 03) REFACTOR - improve the code. 10 | -------------------------------------------------------------------------------- /TestDrivenDevelopment/src/main/java/lk/himash/model/City.java: -------------------------------------------------------------------------------- 1 | package lk.himash.model; 2 | 3 | import lombok.AllArgsConstructor; 4 | import lombok.Data; 5 | import lombok.NoArgsConstructor; 6 | 7 | @AllArgsConstructor 8 | @NoArgsConstructor 9 | @Data 10 | public class City { 11 | 12 | private int id; 13 | private String name; 14 | private String province; 15 | private int population; 16 | 17 | 18 | 19 | } 20 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Test-driven-development 2 | Implement TDD (Test driven development) using Java. 3 | 4 | ## Requirements 5 | 6 | 01) Java 11 7 | 02) Maven 3.8 8 | 9 | ## Project setup 10 | 11 | 01) Clone the project 12 | 13 | https://github.com/himash79/Test-driven-development.git 14 | 15 | 03) Clean and build the project using maven 16 | 17 | open command line (CMD) in project directory and execute 'mvn clean install' 18 | 19 | 04) Open project using intelij / eclipse. 20 | 21 | 05) Test the senarios. 22 | -------------------------------------------------------------------------------- /TestDrivenDevelopment/src/main/java/lk/himash/controller/CityController.java: -------------------------------------------------------------------------------- 1 | package lk.himash.controller; 2 | 3 | import lk.himash.model.City; 4 | import lk.himash.util.CityDetails; 5 | 6 | import java.util.Map; 7 | 8 | public class CityController { 9 | 10 | public String getCityDetails(int id) { 11 | City city = null; 12 | Map cities = CityDetails.setAllCities(); 13 | if(id != 0) { 14 | city = cities.get(id); 15 | } 16 | return city.getName(); 17 | } 18 | 19 | } 20 | -------------------------------------------------------------------------------- /TestDrivenDevelopment/src/main/java/lk/himash/util/CityDetails.java: -------------------------------------------------------------------------------- 1 | package lk.himash.util; 2 | 3 | import lk.himash.model.City; 4 | 5 | import java.util.HashMap; 6 | import java.util.Map; 7 | 8 | public class CityDetails { 9 | 10 | public static Map setAllCities() { 11 | Map cities = new HashMap<>(); 12 | cities.put(1,new City(1,"colombo","weston",10000)); 13 | cities.put(2,new City(2,"galle","southern",4000)); 14 | cities.put(3,new City(3,"kandy","middle",20000)); 15 | cities.put(4,new City(4,"jaffna","north",14000)); 16 | cities.put(5,new City(5,"chilaw","weston",10700)); 17 | return cities; 18 | } 19 | 20 | } 21 | -------------------------------------------------------------------------------- /TestDrivenDevelopment/src/test/java/City.java: -------------------------------------------------------------------------------- 1 | import lk.himash.controller.CityController; 2 | import org.junit.Assert; 3 | import org.junit.Test; 4 | 5 | public class City { 6 | 7 | @Test //RED 8 | public void getCityDetails_test() { 9 | int id = 1; 10 | String expectedOP = "colombo"; 11 | CityController cc = new CityController(); 12 | String realOP = cc.getCityDetails(id); 13 | Assert.assertEquals(expectedOP,realOP); 14 | 15 | } 16 | 17 | @Test(expected = NullPointerException.class) //GREEN 18 | public void getCityDetails_test_error() { 19 | int id = 0; 20 | String expectedOP = "colombo"; 21 | CityController cc = new CityController(); 22 | String realOP = cc.getCityDetails(id); 23 | Assert.assertEquals(expectedOP,realOP); 24 | 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /TestDrivenDevelopment/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 4.0.0 6 | 7 | lk.himash 8 | TestDrivenDevelopment 9 | 1.0-SNAPSHOT 10 | 11 | 12 | 11 13 | 11 14 | 15 | 16 | 17 | 18 | junit 19 | junit 20 | 4.12 21 | test 22 | 23 | 24 | 25 | org.projectlombok 26 | lombok 27 | 1.18.24 28 | provided 29 | 30 | 31 | 32 | 33 | --------------------------------------------------------------------------------