Concordion - verifyRows Command


Advertisements

Concordion verifyRows command can be used to check the content of a collection returned as a result by the system. For example, if we set up a set of users in the system and do a partial search on them, then the system should return the matching elements, otherwise our acceptance tests should fail.

Consider the following requirement −

<table>
   <tr><th>Users</th></tr>
   <tr><td>Robert De</td></tr>
   <tr><td>John Diere</td></tr>
   <tr><td>Julie Re</td></tr>
</table>

<p>Search for J should return:</p>

<table>
   <tr><th>Matching Users</th></tr>
   <tr><td>John Diere</td></tr>
   <tr><td>Julie Re</td></tr>
</table>

If we want write a specification for such a search function which will search and return a collection, then the specification will be as follows −

<table concordion:execute = "addUser(#username)">
   <tr><th concordion:set = "#username">Username</th></tr>
   <tr><td>Robert De</td></tr>
   <tr><td>John Diere</td></tr>
   <tr><td>Julie Re</td></tr>
</table>

<p>Search for "<b concordion:set = "#searchString">J</b>" should return:</p>

<table concordion:verifyRows = "#username : search(#searchString)">
   <tr><th concordion:assertEquals = "#username">Matching Usernames</th></tr>
   <tr><td>John Diere</td></tr>
   <tr><td>Julie Re</td></tr>
</table>

When Concordion parses the document, it will execute addUser() on each row of the first table and then set the searchString to be J. Next, Concordion will execute the search function which should return a Iterable object with a predictable iteration order, (e.g. a List, LinkedHashSet or a TreeSet), verifyRows runs for each item of the collection and runs the assertEquals command.

Example

Let us have a working Eclipse IDE in place and follow the steps given below to create a Concordion application −

Step Description
1 Create a project with a name concordion and create a package com.howcodex under the src folder in the created project.
2 Add the required Concordion libraries using Add External JARs option as explained in the Concordion - First Application chapter.
3 Create Java class System under the com.howcodex package.
4 Create Fixture class SystemFixture under the specs.howcodex package.
5 Create Specification html System.html under the specs.howcodex package.
6 The final step is to create the content of all the Java files and specification file and run the application as explained below.

Here is the content of System.java file −

package com.howcodex;

import java.util.HashSet;
import java.util.Set;
import java.util.SortedSet;
import java.util.TreeSet;

public class System { 
   private Set<String> users = new HashSet<String>();
	
   public void addUser(String username) {
      users.add(username);
   }
	
   public Iterable<String> search(String searchString) {
      SortedSet<String> matches = new TreeSet<String>();
		
      for (String username : users) {
         if (username.contains(searchString)) {
            matches.add(username);
         }
      }
		
      return matches;
   }
}

Following is the content of SystemFixture.java file −

package specs.howcodex;

import org.concordion.integration.junit4.ConcordionRunner;
import org.junit.runner.RunWith;
import com.howcodex.System;

@RunWith(ConcordionRunner.class)

public class SystemFixture {
   System system = new System();
   public void addUser(String username) {
      system.addUser(username);
   }
	
   public Iterable<String> search(String searchString) {
      return system.search(searchString);
   }
}

Following is the content of System.html file −

<html xmlns:concordion = "http://www.concordion.org/2007/concordion">
   <head>
      <link href = "../concordion.css" rel = "stylesheet" type = "text/css" />
   </head>

   <body>
      <h1>System Specifications</h1>
      <p>We are building specifications for our online order tracking application.</p>
      <p>Following is the requirement to add a partial search capability on user names:</p>
		
      <div class = "example">      
         <h3>Example</h3>
			
         <table concordion:execute = "addUser(#username)">
            <tr><th concordion:set = "#username">Username</th></tr>
            <tr><td>Robert De</td></tr>
            <tr><td>John Diere</td></tr>
            <tr><td>Julie Re</td></tr>
         </table>
			
         <p>Search for "<b concordion:set = "#searchString">J</b>" should return:</p>
			
         <table concordion:verifyRows = "#username : search(#searchString)">
            <tr><th concordion:assertEquals = "#username">Matching Usernames</th></tr>
            <tr><td>John Diere</td></tr>
            <tr><td>Julie Re</td></tr>
         </table>
			
      </div> 
		
   </body>

</html>

Once you are done with creating the source and specification files, let us run the application as JUnit Test. If everything is fine with your application, then it will produce the following result −

C:\DOCUME~1\ADMINI~1\LOCALS~1\Temp\concordion\specs\howcodex\System.html
Successes: 2, Failures: 0

System.html is the output of Concordion test run.

concordion verifyRows Output
Advertisements