以编程方式从测试用例中删除try/catch块

问题描述:

我正在为一个研究项目测试测试代码。我们先从测试用例如以编程方式从测试用例中删除try/catch块

@Test 
public void test025() throws Throwable { 
    if (debug) 
     System.out.format("%n%s%n", "RegressionTest1.test025"); 
    java.lang.Double[] d_array2 = new java.lang.Double[] { 1.0d, (-1.0d) }; 
    org.apache.commons.math.linear.ArrayRealVector arrayRealVector3 = new org.apache.commons.math.linear.ArrayRealVector(d_array2); 
    org.apache.commons.math.linear.ArrayRealVector arrayRealVector4 = new org.apache.commons.math.linear.ArrayRealVector(d_array2); 
    try { 
     org.apache.commons.math.linear.ArrayRealVector arrayRealVector7 = new org.apache.commons.math.linear.ArrayRealVector(d_array2, (int) '4', (int) ' '); 
     org.junit.Assert.fail("Expected exception of type org.apache.commons.math.exception.NumberIsTooLargeException"); 
    } catch (org.apache.commons.math.exception.NumberIsTooLargeException e) { 
    } 
    org.junit.Assert.assertNotNull(d_array2); 
} 

断言语句被注释掉.java文件的使用:

original = original.replace("org.junit.Assert.", "//org.junit.Assert."); 
original = original.replace("assert", "//assert"); 

然后,他们被仪表用Javassist记录任何异常(以确保他们发生在测试用例所有运行):

for (CtMethod testmethod : methods) { 
     if (testmethod.getName().startsWith("test")) { 
      try { 
       testmethod.insertBefore("semanticrt.statedump.dumpObjectState.setDumpTestCase(\""+testClassName+ "." + testmethod.getName()+"\");"); 
       CtClass etype = null; 
       try { 
        etype = pool.get("java.lang.Exception"); 
       } catch (NotFoundException e) { 
        e.printStackTrace(); 
       } 

       String exceptionCode = "{ semanticrt.statedump.dumpObjectState.dumpExceptionToFile($e, " + 
         "\"" + outputFileRoot + "."+ testmethod.getName() + ".exception\", false); throw $e; }\n"; 
       testmethod.addCatch(exceptionCode, etype); 
      } catch (CannotCompileException e) { 
       System.out.println(testmethod); 
       e.printStackTrace(); 
      } 

     } 
    } 

其中产量:

@Test 
public void test025() throws Throwable { 
    try { 
     dumpObjectState.setDumpTestCase("RegressionTest1.test025"); 
     if(debug) { 
      System.out.format("%n%s%n", new Object[]{"RegressionTest1.test025"}); 
     } 

     Double[] var1 = new Double[]{Double.valueOf(1.0D), Double.valueOf(-1.0D)}; 
     new ArrayRealVector(var1); 
     new ArrayRealVector(var1); 

     try { 
      new ArrayRealVector(var1, 52, 32); 
     } catch (NumberIsTooLargeException var6) { 
      ; 
     } 

    } catch (Exception var7) { 
     dumpObjectState.dumpExceptionToFile(var7, "/home/loren/repos/d4jBugs/Math_45/version/XMLOut//out-RegressionTest1.test025.exception", false); 
    } 
} 

我的问题是NumberIsTooLargeException被代码中留下的try/catch块所吞噬。 (注意:Exception类可以是任何类,这只是一个有问题的例子。)所以我需要一种方法来在生成测试用例中的任何try/catch块之前将其包装到我的包中。

有没有人知道如何做到这一点与javassist或可能是一个很好的正则表达式,我可以运行在.java文件之前检测删除它们?

我想直到结束:

@Test 
public void test025() throws Throwable { 
    try { 
     dumpObjectState.setDumpTestCase("RegressionTest1.test025"); 
     if(debug) { 
      System.out.format("%n%s%n", new Object[]{"RegressionTest1.test025"}); 
     } 

     Double[] var1 = new Double[]{Double.valueOf(1.0D), Double.valueOf(-1.0D)}; 
     new ArrayRealVector(var1); 
     new ArrayRealVector(var1); 

     new ArrayRealVector(var1, 52, 32); 

    } catch (Exception var7) { 
     dumpObjectState.dumpExceptionToFile(var7, "/home/loren/repos/d4jBugs/Math_45/version/XMLOut//out-RegressionTest1.test025.exception", false); 
    } 
} 
+1

使用'--no-回归assertions'与Randoop应消除需要删除的声明。 – vinegarbin

+0

当然,我们的脚本也能够处理预先编写的测试用例,在这种情况下,我们需要手动对其进行评论。另外,通过运行带有断言的原始测试来确保测试工作正常,这很好。 – Loren

我发现了有效的解决方案。它不会删除try/catch块,但它确实在catch的开头添加了一个throw,因此它提供了所需的功能。

testmethod.instrument(
    new ExprEditor() { 
     public void edit(Handler m) 
       throws CannotCompileException 
     { 
      m.insertBefore("throw $1;"); 
     } 
    } 
); 

这使得整个循环:

for (CtMethod testmethod : methods) { 
     if (testmethod.getName().startsWith("test")) { 
      try { 
       testmethod.instrument(
        new ExprEditor() { 
         public void edit(Handler m) 
           throws CannotCompileException 
         { 
          m.insertBefore("throw $1;"); 
         } 
        } 
       ); 
       testmethod.insertBefore("semanticrt.statedump.dumpObjectState.setDumpTestCase(\""+testClassName+ "." + testmethod.getName()+"\");"); 
       CtClass etype = null; 
       try { 
        etype = pool.get("java.lang.Exception"); 
       } catch (NotFoundException e) { 
        e.printStackTrace(); 
       } 
       // See addCatch() https://jboss-javassist.github.io/javassist/tutorial/tutorial2.html 
       String exceptionCode = "{ semanticrt.statedump.dumpObjectState.dumpExceptionToFile($e, " + 
         "\"" + outputFileRoot + "."+ testmethod.getName() + ".exception\", false); return; }\n"; 
       testmethod.addCatch(exceptionCode, etype); 
      } catch (CannotCompileException e) { 
       System.out.println(testmethod); 
       e.printStackTrace(); 
      } 
     } 
    }