2017-04-07 4 views
0

でテスト記述を作成する方法は、私がJsonListnerであり、junit RunListnerから拡張したものです。 私はそれがテスト実行junit4 runListner

package org.junit.runner; 

import org.junit.runner.notification.RunListener; 

import org.junit.runner.Description; 
import org.junit.runner.Result; 
import org.junit.runner.notification.Failure; 

import java.io.FileWriter; 
import java.io.IOException; 

import org.json.simple.JSONArray; 
import org.json.simple.JSONObject; 

class JsonListener extends RunListener { 

    private JSONObject _tests_passed = new JSONObject(); 
    private JSONObject _tests_started = new JSONObject(); 
    private JSONObject _tests_finished = new JSONObject(); 
    private JSONObject _tests_failures = new JSONObject(); 
    private JSONObject _tests_ignored = new JSONObject(); 

    public void testRunStarted(Description description) throws Exception { 

    } 

    public void testRunFinished(Result result) throws Exception { 
    System.out.println(_tests_passed); 
    } 

    public void testStarted(Description description) throws Exception { 
    String key = description.getDisplayName(); 
    long value = System.currentTimeMillis(); 

    _tests_started.put(key, value); 
    } 

    public void testFinished(Description description) throws Exception { 
    // trying to prit the description of the test running 
    System.out.println(palindromeStringTest.desc); 
    } 

    public void testFailure(Failure failure) throws Exception { 
    String key = failure.getDescription().getDisplayName(); 
    long value = System.currentTimeMillis(); 

    _tests_failures.put(key, value); 
    } 

    public void testAssumptionFailure(Failure failure) { 
    String key = failure.getDescription().getDisplayName(); 
    long value = System.currentTimeMillis(); 

    _tests_failures.put(key, value); 
    } 

    public void testIgnored(Description description) throws Exception { 
    String key = description.getMethodName(); 
    long value = System.currentTimeMillis(); 

    _tests_ignored.put(key, value); 
    } 

} 

これは私がランナーを通じて実行しているテストクラスの一つである後JSONレポートを取得使用しています。

import java.io.*; 
import org.junit.After; 
import org.junit.AfterClass; 
import org.junit.Before; 
import org.junit.BeforeClass; 
import org.junit.Test; 
import static org.junit.Assert.*; 

public class palindromeStringTest { 

    ByteArrayOutputStream outContent = new ByteArrayOutputStream(); 
    static String desc; 

    @Before 
    public void setUpStream() { 
    System.setOut(new PrintStream(outContent)); 
    } 

    @After 
    public void cleanUpStream() { 
    System.setOut(new PrintStream(new FileOutputStream(FileDescriptor.out))); 
    } 

    @Test 
    public void revTest() { 
    palindromeStringTest.desc = "this should reverse the string"; 
    String a = "Madam"; 
    palindromeString obj = new palindromeString(); 
    String b = obj.rev(a); 
    assertEquals("madaM", b); 
    } 
} 

私の質問は、私はrunListnerのtestFinished方法でthis.desc値を得るのですかですか? これは、各テストの実行が完了した後で説明フィールドの値を取得するために使用することを考えた代替方法です。上記の実装で は、私はランナー

Buildfile: /home/bonnie/WorkStation/JUnit-Json-Runner/build.xml 

clean: 
    [delete] Deleting directory /home/bonnie/WorkStation/JUnit-Json-Runner/build 
    [delete] Deleting directory /home/bonnie/WorkStation/JUnit-Json-Runner/dist 

mkdir: 
    [mkdir] Created dir: /home/bonnie/WorkStation/JUnit-Json-Runner/build/classes 
    [mkdir] Created dir: /home/bonnie/WorkStation/JUnit-Json-Runner/dist 

compile: 
    [javac] Compiling 2 source files to /home/bonnie/WorkStation/JUnit-Json-Runner/build/classes 
    [javac] /home/bonnie/WorkStation/JUnit-Json-Runner/src/JsonListner.java:39: error: cannot find symbol 
    [javac] System.out.println(palindromeStringTest.desc); 
    [javac]     ^
    [javac] symbol: variable palindromeStringTest 
    [javac] location: class JsonListener 
    [javac] Note: /home/bonnie/WorkStation/JUnit-Json-Runner/src/JsonListner.java uses unchecked or unsafe operations. 
    [javac] Note: Recompile with -Xlint:unchecked for details. 
    [javac] 1 error 

BUILD FAILED 
/home/bonnie/WorkStation/JUnit-Json-Runner/build.xml:22: Compile failed; see the compiler error output for details. 

Total time: 1 second 

方法はどのようなものをコンパイルに失敗しています、これはacomplishedすることができますか?ランナーにテスト記述を作成して取得するための既存の方法がありますか?

答えて

0

あなたは、説明変数にアクセスするためにJsonListnerjava.lang.reflect.*を使用する必要があります。

palindromeStringTestクラスのメソッドgetDescritionを作成し、説明を返します。

import java.io.*; 
import org.junit.After; 
import org.junit.AfterClass; 
import org.junit.Before; 
import org.junit.BeforeClass; 
import org.junit.Test; 
import static org.junit.Assert.*; 

public class palindromeStringTest { 

    ByteArrayOutputStream outContent = new ByteArrayOutputStream(); 
    static String desc; 

    //public method to get the value of desc 
    public function getDescription(){ 
    return desc; 
    } 

    @Before 
    public void setUpStream() { 
    System.setOut(new PrintStream(outContent)); 
    } 

    @After 
    public void cleanUpStream() { 
    System.setOut(new PrintStream(new FileOutputStream(FileDescriptor.out))); 
    } 

    @Test 
    public void revTest() { 
    palindromeStringTest.desc = "this should reverse the string"; 
    String a = "Madam"; 
    palindromeString obj = new palindromeString(); 
    String b = obj.rev(a); 
    assertEquals("madaM", b); 
    } 
} 

今、あなたは、Java reflectionクラス

public void testFinished(Description description) throws Exception { 
    Class<?> testClass = description.getTestClass(); 
    Method m = testClass.getDeclaredMethod("getDescription"); 
    Object o = m.invoke(null); 
    System.out.println(o.toString()); 
    } 
の助けを借りて testFinished方法で descriptionにアクセスすることができます
0

palindromeStringTest変数が定義されていないので、あなたは方法

public void testFinished(Description description) throws Exception { 
    // trying to prit the description of the test running 
    System.out.println(palindromeStringTest.desc); 
    } 

でコンパイルエラーを取得しています。

次に、リスナーを起動するには、JUnitCoreでテストを実行する必要があります。

public void main(String... args) { 
    JUnitCore core= new JUnitCore(); 
    core.addListener(new JsonListener()); 
    core.run(MyTestClass.class); 
} 
関連する問題