![](/img/trans.png)
[英]How to test a CursorLoader with AndroidJUnitRunner and JUnit4?
[英]how to create a test description with junit4 runListner
以下是我的JsonListner
我从JUnit的扩展RunListner
。 我正在使用它在测试运行后得到一个json报告
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);
}
}
这是我正在跑步者中进行的测试课程之一。
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);
}
}
我的问题是如何在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
有什么方法可以实现? 是否已经有一种方法可以在运行器中创建和获取测试说明?
您在方法中遇到编译错误
public void testFinished(Description description) throws Exception {
// trying to prit the description of the test running
System.out.println(palindromeStringTest.desc);
}
由于未定义palindromeStringTest
变量。
然后,要调用您的侦听器,您需要通过JUnitCore运行测试。
public void main(String... args) {
JUnitCore core= new JUnitCore();
core.addListener(new JsonListener ());
core.run(MyTestClass.class);
}
您必须在JsonListner
中使用java.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
类访问testFinished
方法中的description
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());
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.