javagroovybddspock

How to validate each entry of arraylist of Java in a Groovy test case


We are using Spock framework (Groovy language) to write test cases for our Java code.

I am new to Groovy and Spock framework.

I am trying to validate values contained in a hashmap

I have a class like this:

public class Properties{

protected List<Pair> property;

public List<Pair> getProperty() {
        if (property == null) {
            property = new ArrayList<Pair>();
        }
        return this.property;
    }

}

public class Pair {

protected String name;
protected String surname;

...getters and setters

}

In the Spock framework, the test case is as follows:

def "Test case"(){

given:

....
....

when:
...
...

then:
...

def pairs = getProperties().getProperty()

pairs.each {

pair -> 
            if (pair.getName().equals("Anand")){
                pair.getSurname().equals("Zaveri")
                println "a"
            }
            if (pair.getName().equals("Rohit")){
                pair.getSurname().equals("Sharma")
            }

}
where:
...

}

Now when the name is Rohit, the list contains surname is Kapoor and I am comparing it with Sharma, but still the test case passed. What am I doing wrong here?

The list contains a lot of names and I want to check their surnames in this test case.


Solution

  • Add assert to each comparison e.g.

    assert pair.getSurname().equals("Zaveri")