I have a class with some methods:
public class A{
public void method1(){
}
public void method2(){
}
}
And then, I wrote some test methods to check the corresponding ones, for example:
public class ATest{
@Test
public void testMethod1(){
....
assertTrue(...);
.....
assertEquals(...);
}
@Test
public void testMethod2(){
}
}
After that, I know that a test method (ex: testMethod1) should just only tests one thing, so I decided to write more methods for each cases. It looks like the following:
@Test
public void testMethod1_When_Case1(){
....
assertTrue(...);
}
@Test
public void testMethod1_When_Case2(){
....
assertEquals(...);
}
However, it was not a really good approach because it seems that I just focused on test the functionality of the method of the class. With TDD approach, I knew that I should test the behaviors of a class, not the methods belong to this class. Because, we all know the class' behaviors is represented the behaviors of our system.
Therefore, I would like to turn to behavior testing approach; and BDD (behaviour-driven development) is my response. Here is it:
- From the requirements, I call them are user stories in Agile. Something likes:
As a [Role]I define some acceptance tests in order to know what exactly its values is. These acceptance tests are represented the behaviors of my system. Somethings likes:
I want [Feature]
So that I receive [Value]
Given [Context]- From the acceptance tests, I started to build a class that implements the behaviors. With TDD and BDD approach together, I start writing test code first. For example:
When [Event Occurs]
Then [Outcome]
@Test
public void shouldReceiveOrDoSomethingWhenEvent1Occurs(){
}
@Test
public void shouldReceiveOrDoSomethingWhenEvent2Occurs(){
}
Yeah, that was all. Now, I follow this approach and I have a test case that represened for a behavior of a class, not only test for a method as before.
References:
[1]. http://dannorth.net/introducing-bdd/
[2]. http://www.ryangreenhall.com/articles/bdd-by-example.html