What is TDD?
If somebody asks me to explain TDD in few words, I say TDD is a development of tests before a feature implementation. You can argue that it’s hard to test things which are not existing yet. And Kent Beck will probably give you a slap for this.
If somebody asks me to explain TDD in few words, I say TDD is a development of tests before a feature implementation. You can argue that it’s hard to test things which are not existing yet. And Kent Beck will probably give you a slap for this.
So how is it possible? It can be described by following steps:
AllTests.java
- You read and understand requirements for a particular feature.
- You develop a set of tests which check the feature. All of the tests are red, due to absence of the feature implementation.
- You develop the feature until all tests become green.
- Refactor the code.
package SMD7;
import org.junit.runner.RunWith;
import org.junit.runners.Suite;
import org.junit.runners.Suite.SuiteClasses;
@RunWith(Suite.class)
@SuiteClasses({ DoctorTest.class, ReceptionistTest.class })
public class AllTests {
}
Receptionist.java
package SMD7; public class Receptionist { private int id =101; private String name = "Raju"; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } }ReceptionistTest.java
package SMD7;
import static org.junit.Assert.*;
import org.junit.Test;
public class ReceptionistTest {
@Test
public void test() {
Receptionist R = new Receptionist();
int id = R.getId();
String name = R.getName();
assertEquals(101, id);
assertEquals("Raju", name);
}
}
Doctor.java
package SMD7;
public class Doctor {
private int id =201;
private String name = "Rastogi";
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
DoctorTest.java
package SMD7; import static org.junit.Assert.*; import org.junit.Test; public class DoctorTest { @Test public void test() { Doctor R = new Doctor(); int id = R.getId(); String name = R.getName(); assertEquals(201, id); assertEquals("Rastogi", name); } }This video might be helpful.
Comments
Post a Comment