Monday, June 7, 2010

Exact meaning of GIVEN/THEN/WHEN sections

I was recently wondering about exact meaning of given/when/then sections in the context of the following test case template for web service:
  1. create service instance for testing
  2. prepare request
  3. send request and receive response
  4. validate response
My concern was... where to put the code preparing a request? Does it belong to GIVEN or WHEN?
@Test
   public void shouldNotSellIfInhibitRule() throws Exception {
      // given
      SellDataRetriever retriever = createRetrieverWithFixedBookResponse("sell_inhibit_rule_rsp.xml");
      SellRQ anyValidRequest = createMinimalValidSellRequest();      
      // when
      SellRS response = retriever.sell(anyValidRequest);
      // then
      assertEquals(ResponseStatus.FAILURE, response.getResponseStatus());
   }
or
@Test
   public void shouldNotSellIfInhibitRule() throws Exception {
      // given
      SellDataRetriever retriever = createRetrieverWithFixedBookResponse("sell_inhibit_rule_rsp.xml");
      // when
      SellRQ anyValidRequest = createMinimalValidSellRequest();      
      SellRS response = retriever.sell(anyValidRequest);
      // then
      assertEquals(ResponseStatus.FAILURE, response.getResponseStatus());
   } 
Although I haven’t found the right answer yet here's some hint:
http://blog.objectmentor.com/articles/2008/11/27/the-truth-about-bdd

GIVEN should set the SUT to some fixed state. WHEN should change that state. Based on these two I think that preparing the request is part of the code responsible for state transition and thus it belongs to WHEN. In my opinion the second solution is better :-)

No comments:

Post a Comment