Thursday, September 24, 2015

How did I start practising BDD?

At the beginning days I has practised TDD (Test Driven Development) with using JUnit, I approached that I should test methods belong to a class. For example:

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 want [Feature]
So that I receive [Value]
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:
Given [Context]
When [Event Occurs]
Then  [Outcome]
- 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:

@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

No comments:

Post a Comment