您的位置:軟件測試 > 開源軟件測試 > 開源單元測試工具 > junit
使用Grester簡化Java應(yīng)用程序的JUnit測試
作者:網(wǎng)絡(luò)轉(zhuǎn)載 發(fā)布時間:[ 2013/3/22 14:37:43 ] 推薦標(biāo)簽:

清單 3. 在 Maven 項目中測試的示例類

               
package com.prometheus.run;
import java.io.IOException;
import java.io.InputStream;
public class CommandExecutor extends Executor{
    ...
    public String executeCommand(String command){
  ...
        try {
            Process child = performCommandExecution(command);
            stream = child.getInputStream();
            sb = processStream(stream);
            ...
        }
            ...
        return sb.toString();
    }
    protected StringBuffer processStream(InputStream stream) throws IOException {
         ...
         sb = new StringBuffer();
        while ((c = stream.read()) != -1) {
                sb.append((char)c);
                }
        return sb;
    }
   ...
}


在 CommandExecutor 類中,executeCommand() 方法將調(diào)用同一個類 processStream() 中的受保護方法。在 processStream() 方法中,將在 while() 循環(huán)中創(chuàng)建一個新 StringBuffer 實例并且處理 InputStream。清單 4 顯示了測試類,還顯示了測試的主要部分。

清單 4. Maven 項目中的示例測試類

               
                package com.prometheus.run;
import com.prometheus.run.CommandExecutor;
...
public class CommandExecutorTest extends TestCase {
 ...
 public class MockProcess extends Process{
        ...
        public InputStream getInputStream(){
            String source= "This is a mock string";
            return new ByteArrayInputStream(source.getBytes());
          }
        public OutputStream getOutputStream(){
            return null;
        }
        public int waitFor(){
            return 1;
        }
    }
 public void testExecuteCommmand(){
        String expected = "This is a mock string";
        String actual = commandExecutor.executeCommand("lsmod");
        assertEquals(expected, actual);
        ...
    }
  }


測試類 CommandExecutorTest 相對簡單。雖然給出的詳細信息不多,但是此單元測試的基本目標(biāo)是在測試時通過類的 performCommandExecution() 方法調(diào)用來模擬 Process 類的行為。

必須注意的是,要讓 Grester 成功運行,項目必須編譯代碼源文件和測試源文件并成功運行任意一個測試和所有測試(注:由于這個原因,test-compile Maven 階段將標(biāo)記允許 Grester 運行且不能提前運行的階段)。下一步是簡單地在項目的 pom.xml 文件中附加 Grester 的 Maven 插件配置。此配置放在 pom.xml 文件的默認(rèn)構(gòu)建部分中或任何常規(guī)的 Maven 配置文件中。

把 Grester 與項目聯(lián)系在一起

清單 5 顯示了放在示例項目的 pom.xml 文件中的 Grester 插件的示例配置。注意,groupId 要對應(yīng)于 org.apache.Maven.plugins 并且版本應(yīng)該是新的 Grester 插件:V0.3。

清單 5. 示例項目中的 Grester 插件配置

               
<plugins>
...
...
<!-- START MAVEN GRESTER PLUG-IN CONFIGURATION -->
<plugin>
<groupId>org.apache.Maven.plugins</groupId>
<artifactId>Maven-Grester-plugin</artifactId>
<version>0.3</version>
<configuration>
<codeSources>src/main/java/com/prometheus/run</codeSources>
<testSuiteClass>com.prometheus.run.CommandExecutorTest</testSuiteClass>
</configuration>
<executions>
<execution>
<id>inspectSourcesCodeWithGrester</id>
<phase>test</phase>
<goals>
<goal>inspect</goal>
</goals>
</execution>
</executions>
</plugin>
<!-- END MAVEN GRESTER PLUG-IN CONFIGURATION -->
...
</plugins>


注意,項目已被設(shè)為在 Maven 的測試階段運行 Grester 的 inspect 目標(biāo)。codeSources 將指向包含測試類 CommandExecutorTest 的源代碼的目錄。它可以像簡單地指向?qū)嶋H類 CommandExecutor 一樣排除文件擴展名。在 Grester 附帶的 README.txt 文件中提到了擴展名 .Groovy,但是應(yīng)當(dāng)注意的是,目前沒有對 Grester 的支持。

上一頁12345下一頁
軟件測試工具 | 聯(lián)系我們 | 投訴建議 | 誠聘英才 | 申請使用列表 | 網(wǎng)站地圖
滬ICP備07036474 2003-2017 版權(quán)所有 上海澤眾軟件科技有限公司 Shanghai ZeZhong Software Co.,Ltd