Let me show u simple TDD....
just a simple class student with its test class named studentTest.
1. create the StudentTest class.
public class StudentTest extends TestCase
{
public void testCreate()
{
Student student = new Student("peysal");
}
}
this test will sure fail becoz we haven't create the student class. So we create the student class
2. create the Student class
public class Student
{
public Student(String name)
{
}
}
then try running the automated test, guarantee ok
3. assert what u expected, this 1st assert must fail
public class StudentTest extends TestCase
{
public void testCreate()
{
Student student = new Student("peysal");
assert("peysal", student.getName());
}
}
as u can see, we haven't create the function of student.getName. So u can expect failure when u run the automated test
4. implement the missing function in Student class
public class Student
{
private String name;
public Student(String name)
{
this.name = name;
}
public String getName()
{
return name;
}
}
thats it...u should be getting the green colur. Ok and well done.
Even though, writing automated test is fun. I just don feel comfortable writing code 1st then getting all the syntax error for missing function or what so ever. So maybe i'll adopt TDD style to some extend only, for now. Getting failure in automated test is ok, but getting the error for me....alaaa lecehnya. I want to skip that part.
No comments:
Post a Comment