View Ravichandran J.V.'s profile on LinkedIn RJV 's Blog: November 2012 Tweet @ravichandranjv

Translate

Friday, 30 November 2012

At Patna for the OLPC Demo - 27th - 29th Nov 2012

Demo of the OLPC Laptop



Leadership Training on the Use of ICT in Teacher Education in Bihar: Strategic Workshop



Demo of the OLPC laptop 


Wednesday, 14 November 2012

BDD with NBehave starter example

Here is a quick help for BDD with NBehave beginners.

Also refer to this presentation by me on BDD and UADD at the Agile NCR conference  - BDD and UADD with Slim

The .feature text  file that describes the scenario(s) and which gets executed by NBehave !


The ActionSteps that define the actions proposed in the .feature file above in the BDD format of Given, When, Then.


And the production class,

 public class Calculator
    {
        private int result;
        private int one, two;
        public void Enter(int o,int t)
        {
            one = o;
            two = t;
        }
        
        public void Add()
        {
            result = one + two;
        }
        public int Value() {
           return result; 
        }
    }

Below is the nbehave results



Monday, 12 November 2012

Behavior Driven Development with Tumbler

Will elaborate on comments and questions to this post :)

More on this can be found at - http://ravichandranjv.blogspot.in/2010/05/decision-table-in-slim.html

Also refer to this presentation by me on BDD and UADD at the Agile NCR conference  - http://tinyurl.com/d6xjfox


import static tumbler.Tumbler.*;

import org.junit.runner.Result;
import org.junit.runner.RunWith;
import static org.junit.Assert.*;
import tumbler.Scenario;
import tumbler.Story;
import org.hamcrest.BaseMatcher;
import org.hamcrest.core.IsEqual;
import org.hamcrest.core.IsNot;
import org.hamcrest.core.IsNull;
import org.hamcrest.core.IsSame;
import tumbler.JUnit44TumblerRunner;
import tumbler.TumblerRunner;

@RunWith(JUnit44TumblerRunner.class)
@Story("Generate genome sequence from values passed")
public class GenomeTest {
@Scenario("ATGAC")
    public void shouldGenerateCorrectPatern() {
        Given("A Genome sequence");
        GenomePattern gp = new GenomePattern();
        Object someValue=null;

        When("Genome pattern is to be generated");
        Result result = gp.generatePattern();

        Then("expect ATGAC");
        assertNull(result); // Making test pass as per TDD
    }
}

Sunday, 11 November 2012

Parameterized test with HashMap example - JUnit


A rather contrived example but since there is a dearth of JUnit example on parametrized tests on the web it should help plus JUnit parameters is itself a little contrived ! :) Theories and Datapoints are a better option...will post soon on Theories.

package parameterTests;

import static org.junit.Assert.*;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;

@RunWith(value=Parameterized.class)
public class ParameterAnnotation {
private Map geo;
      @Parameters
public static Collection configs() { Map geo1 = new HashMap(); geo1.put("Country", "India"); geo1.put("Capital", "New Delhi"); return Arrays.asList(new Object[][] { { geo1 } }); }      public ParameterAnnotation(Map geo) {
        this.geo = geo;
    }

 @Test
public void pushTest1() {
    assertEquals("India", geo.get("Country"));
    assertEquals("New Delhi", geo.get("Capital"));
}
}

Parameterized test example - JUnit


import static org.junit.Assert.*;
import java.util.Arrays;
import java.util.List;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;

@RunWith(value=Parameterized.class)
public class ParameterAnnotation {
private int number;
public ParameterAnnotation(int number) {
   this.number = number;
}
private static int currentIndex, nextIndex;
@BeforeClass
public static void initClassValues(){
currentIndex=0;
nextIndex=0;
}
private static Object[][] data=new Object[5][1];

@Parameters
public static List data() {
       data[0][0]=1;
       data[1][0]=2;
       data[2][0]=3;
       data[3][0]=4;
       data[4][0]=5;
       return Arrays.asList(data);
}
@Test
public void pushTest() {
assertEquals(number, data[currentIndex][nextIndex]);
currentIndex++;
}
}

Friday, 9 November 2012

JUnit and MockItTo test (Arrange, Act, Assert & Verify)

A simple scenario of an assert and a setting expectation with MockItTo:


1. @Test
2. public void call_should_return_hello_world(){
// Arrange
3. HelloWorld h=mock(HelloWorld.class);
// Act
4. when(h.sayHello()).thenReturn("Hello World");
// Assert
5. assertEquals("Hello world", h.sayHello());
// Verify
6. verify(h).sayHello();
}

Line 4 sets the expectation (a la BDD style), that when sayHello() is called the value returned should be the string passed to thenReturn.

Line 5 performs an assertion, totally independent of the mock expectation set.

Line 6 verifies that the method call was made on the mock object "h", at least once, the default.

So, what does this example serve for? It helps as an example to differentiate between the expectation of a JUnit assert from a mock expectation regardless of the type of object on which the method call is made.

Comment Line 5 and Line 6 will fail with

"Wanted but not invoked:
helloWorld.sayHello();
-> at Hello_Mockito.call_should_return_hello_world(Hello_Mockito.java:34)
Actually, there were zero interactions with this mock.

Simplify the code with annotations:


@RunWith(MockitoJUnitRunner.class)
public class Hello_Mockito {
// Arrange
@Mock
HelloWorld h;
@Test
public void call_should_return_hello_world(){
//HelloWorld h=mock(HelloWorld.class);
                        // Act
when(h.sayHello()).thenReturn("Hello World");
                       // Assert
assertEquals("Hello World", h.sayHello());
           // Verify
                       verify(h).sayHello();
}
}