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 test one thing, so I decided to write more methods for each case. 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 the TDD approach, I knew that I should test the behaviors of a class, not the methods that belong to this class. Because we all know the class's behaviors have represented the behaviors of our system.
Therefore, I would like to turn to a behavior testing approach; and BDD (behavior-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 their values are. These acceptance tests have 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 represented the behavior of a class, not only the test for a method as previously.
References:
[1]. http://dannorth.net/introducing-bdd/
[2]. http://www.ryangreenhall.com/articles/bdd-by-example.html
Comments
Post a Comment