Concordion - 返回Map



Concordion execute 命令可以用來獲取行為結果,結果以 Map 的形式呈現,我們可以從中獲取行為的多個輸出。例如,考慮以下需求:

The full name Robert De is to be broken into its first name Robert and last name De.

這裡我們需要一個 split 函式,它接受一個使用者名稱並返回一個 Map 物件,該物件包含 firstName 和 lastName 作為鍵,並具有相應的價值,以便我們可以使用它們。

如果我們想為這樣的 split 函式編寫一個規範,該函式將接受一個使用者名稱並輸出一個結果物件,那麼以下將是規範:

<p>The full name <span concordion:execute = "#result = split(#TEXT)">Robert 
   De</span> is to be broken into first name <span 
   concordion:assertEquals = "#result.firstName">Robert</span> and last name <span 
   concordion:assertEquals = "#result.lastName">De</span>.</p>

當 Concordion 解析文件時,它將把特殊變數 #TEXT 的值設定為當前元素的值“Robert De”,並將其傳遞給 split 函式。然後,它將使用 execute 命令使用 #TEXT 作為引數執行 split() 方法,並將結果設定為 #result 變數,並使用結果對映列印 firstName 和 lastName 值作為輸出。

示例

讓我們準備一個可用的 Eclipse IDE,並按照以下步驟建立 Concordion 應用程式:

步驟 描述
1 建立一個名為 concordion 的專案,並在建立的專案中的 src 資料夾下建立一個包 com.tutorialspoint
2 使用 新增外部 JAR 選項新增所需的 Concordion 庫,如 Concordion - 第一個應用程式 章節中所述。
3 com.tutorialspoint 包下建立 Java 類 System
4 specs.tutorialspoint 包下建立 Fixture 類 SystemFixture
5 specs.tutorialspoint 包下建立規範 html 檔案 System.html
6 最後一步是建立所有 Java 檔案和規範檔案的內容,並按如下所述執行應用程式。

以下是 System.java 檔案的內容:

package com.tutorialspoint;

import java.util.HashMap;
import java.util.Map;

public class System {
   public Map split(String userName){
      Map<String, String> result = new HashMap<String, String>();
      String[] words = userName.split(" ");
      result.put("firstName", words[0]);
      result.put("lastName", words[1]);
      return result;
   }
}

以下是 SystemFixture.java 檔案的內容:

package specs.tutorialspoint;

import java.util.Map;
import com.tutorialspoint.Result;
import com.tutorialspoint.System;
import org.concordion.integration.junit4.ConcordionRunner;
import org.junit.runner.RunWith;

@RunWith(ConcordionRunner.class)

public class SystemFixture {
   System system = new System();
   public Map<String, String> split(String userName){
      return system.split(userName);
   }  
}

以下是 System.html 檔案的內容:

<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 split full name of a logged in user to its 
         constituents by splitting name by whitespace:</p>
			
      <div class = "example">      
         <h3>Example</h3>
         <p>The full name <span concordion:execute = "#result = split(#TEXT)">Robert 
            De</span> is to be broken into first name <span 
            concordion:assertEquals = "#result.firstName">Robert</span> and last name 
            <span concordion:assertEquals = "#result.lastName">De</span>.</p>
      </div>
		
   </body>

</html>

建立原始檔和規範檔案後,讓我們將其作為 JUnit 測試執行。如果您的應用程式一切正常,則會產生以下結果:

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

System.html 是 Concordion 測試執行的輸出。

concordion Returning Map Output
廣告

© . All rights reserved.