JUnit - 如何构造测试对象

时间:2014-06-19 05:52:03

标签: junit

我们的测试框架基于Junit 3.x。

如果我可以执行junit 4测试用例,我正在尝试检查当前框架。

在我们的框架中,从命令行或UI,TestServlet

执行测试用例有多种方法

1)通过Testsuite - 完全限定名称 2)通过Testsuite(完全限定名称)和个别测试用例(从套件中执行一个测试用例)..

我正在尝试在我们的专有框架中使用Juint4测试,该框架使用了联合3.x)

我被困在一块。

新的测试用例在Junit 4中并使用JUnit4TestAdapter。

问题:当我知道带注释的junit 4方法时,如何构造测试对象。<​​/ p>

Class suiteClass = ClassUtil.classForName(suiteName);

Object o = ClassUtil.newInstance(suiteClass);

//the suite name passed contains junit4 tests

if ( !(o instanceof TestSuite)&& isjunit4TestCase(o))
{

       // see if the method exists in the class

       Method method;

       try {
        method = suiteClass.getMethod(testName);
       } catch (NoSuchMethodException e) {
   Log.harness.debug("method: %s not found in %s",testName, nameToUse);
   return null;
 }

 Test m_test = null;

 if (method.isAnnotationPresent(org.junit.Test.class))
 {
    /*here I need to construct a Test Object  and stuck
   junit.framework.Test  need this object to be returned
   */

m_test = (Test)method;

return m_test;
 }
 else
return null;

1 个答案:

答案 0 :(得分:0)

您可以使用JUnit4TestAdapter.filter(Filter)吗?然后,您可以选择编写自己的过滤器或使用Filter.matchMethodDescription(Description)。使用createTestDescription(Class<?> testClass, String methodName)静态工厂方法时,匹配的描述变得非常简单。

因为JUnit4TestAdapter本身实现了Test,所以您只需将过滤后的JUnit4TestAdapter直接传递给需要Test实例的方法即可。 只需将JUnit4测试类本身传递给JUnit4TestAdapter构造函数,就可以轻松创建实例。

(事实上,你可以避免在你的答案中有一些反思,并把它交给JUnit代替。)

相关问题