JUnit4TestClassReference

JUnit4TestClassReference

本文介绍了TestNG可以看到我的Spock(JUnit)测试结果吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 由于TestNG支持运行JUnit测试,因此我尝试了这种方法: <!DOCTYPE suite SYSTEMhttp://testng.org/testng-1.0.dtd> < test name =myTestName> < classes> < class name =mypackage.artifact.SomeArtifactSpecification/> < / classes> < / test> < / suite> 所以这实际上完美地运行了Groovy的'SomeArtifactSpecification'Spock单元测试。我可以看到一些来自测试的I / O,但是由于我在Eclipse中运行,我使用调试器来验证所有测试都正常执行。我的类路径上有TestNG-6.8.7和JUnit-4.10。问题是TestNG似乎并不知道它做到了!这里是结束输出: ==================== =========================== myTestName 总测试运行:0,失败:0,跳过:0 =============================================== 有谁知道是否可以让TestNG识别这些测试? 其他详情 - 为赏金添加 在下面的评论之一中,Peter表示, TestNG正在做什么。我尽可能多地调查过。似乎Junit和TestNG都会调用Spock的Sputnik类63行的'run'方法。但是,当从JUnit直接调用时,Sputnik将从JUnit4TestClassReference中调用。从TestNG调用时,Sputnik从JUnitCore中调用。 所以问题仍然存在:如何让TestNG识别Spock的测试输出?如果我要活得长久和繁荣,我真的需要知道!看起来答案可能是让TestNG与JUnit4TestClassReference一起运行,或者使用某种巧妙的包装来使TestNG做到这一点...... ,我在下面发布了两个堆栈跟踪。第一个来自TestNG run,它不起作用,第二个来自没有TestNG直接运行Spock。 $ b 来自TestNG的第一个堆栈跟踪: > org.testng.remote.RemoteTestNG at localhost:54288 Thread [main](Suspended(breakpoint at line ) ArtifactManagerSpecification。$ spock_feature_0_0()line:65 NativeMethodAccessorImpl.invoke0(方法,对象,对象[])行:不可用[本机方法] NativeMethodAccessorImpl.invoke对象,对象[])行:57 DelegatingMethodAccessorImpl.invoke(Object,Object [])行:43 Method.invoke(Object,Object ...)行:601 ReflectionUtil。 invokeMethod(Object,Method,Object ...)行:138 ParameterizedSpecRunner(BaseSpecRunner).invokeRaw(Object,MethodInfo,Object ...)行:330 ParameterizedSpecRunner(BaseSpecRunner).invoke(Object, MethodInfo,Object ...)行:311 ParameterizedSpecRunner(BaseSpecRunner).invokeFeatureMethod()line:285 ParameterizedSpecRunner(BaseSpecRunner).doRunIteration()line:256 NativeMethodAccessorImpl.invoke0(Method,Object,Object [])line:not available [native method] NativeMethodAccessorImpl.invoke(Object,Object [])行:57 DelegatingMethodAccessorImpl.invoke(Object,Object [])行:43 Method.invoke(Object,Object ...)行:601 ReflectionUtil.invokeMethod(Object,Method,Object ...)行:138 ParameterizedSpecRunner(BaseSpecRunner).invokeRaw(Object,MethodInfo,Object ...)行:330 ParameterizedSpecRunner (BaseSpecRunner).invoke(Object,MethodInfo,Object ...)行:311 ParameterizedSpecRunner(BaseSpecRunner).runIteration(Object [],int)行:223 ParameterizedSpecRunner(BaseSpecRunner).initializeAndRunIteration(Object [],int)行:214 ParameterizedSpecRunner(BaseSpecRunner).runSimpleFeature()行: 205 ParameterizedSpecRunner(BaseSpecRunner).doRunFeature()行:199 NativeMethodAccessorImpl.invoke0(方法,对象,对象[])行:不可用[本机方法] NativeMethodAccessorImpl.invoke(Object,对象[])行:57 DelegatingMethodAccessorImpl.invoke(Object,Object [])行:43 Method.invoke(Object,Object ...)行:601 ReflectionUtil.invokeMethod(对象,方法,对象...)行:138 ParameterizedSpecRunner(BaseSpecRunner).invokeRaw(Object,MethodInfo,Object ...)line:330 ParameterizedSpecRunner(BaseSpecRunner).invoke(Object,MethodInfo,对象...)行:311 ParameterizedSpecRunner(BaseSpecRunner).runFeature()line:175 ParameterizedSpecRunner(BaseSpecRunner).runFeatures()line:152 ParameterizedSpecRunner(BaseSpecRunner).doRunSpec行:112 NativeMethodAccessorImpl.invoke0(方法,对象,对象[])行:不可用[本机方法] NativeMethodAccessorImpl.invoke(Object,Object [])行:57 DelegatingMethodAccessorImpl.invoke(Object,Object [])行:43 Method.invoke(Object,Object ...)行: b $ b ReflectionUtil.invokeMethod(Object,Method,Object ...)行:138 ParameterizedSpecRunner(BaseSpecRunner).invokeRaw(Object,MethodInfo,Object ...)行:330 ParameterizedSpecRunner(BaseSpecRunner) .invoke(Object,MethodInfo,Object ...)行:311 ParameterizedSpecRunner(BaseSpecRunner).runSpec()行:91 ParameterizedSpecRunner(BaseSpecRunner).run()行:82 Sputnik .run(RunNotifier)行:63 JUnitCore.run(Runner)行:157 JUnitCore.run(请求)行:136 JUnit4TestRunner.start(Class,String ...)行:81 JUnit4TestRunner.run(Class,String ...)行:69 TestRunner $ 1.run()line:682 TestRunner.runWorkers(List< IWorker< ITestNGMethod>> ;,字符串,ListMultiMap&l t; Integer,TestMethodWorker>)行:1005 TestRunner.privateRunJUnit(XmlTest)行:713 TestRunner.run()行:614 SuiteRunner.runTest(TestRunner)行:334 SuiteRunner.runSequentially()行:329 SuiteRunner.privateRun()行:291 SuiteRunner.run()行:240 SuiteRunnerWorker.runSuite(SuiteRunnerMap,XmlSuite)行:52 SuiteRunnerWorker.run()行: RemoteTestNG(TestNG).runSuitesSequentially(XmlSuite,SuiteRunnerMap,int,String)行:1224 RemoteTestNG(TestNG).runSuitesLocally()行:1149 RemoteTestNG(TestNG).run()行:1057 RemoteTestNG.run()行:111 RemoteTestNG.initAndRun(String [],CommandLineArgs,RemoteArgs)行:204 RemoteTestNG。 main(String [])行:175 运行Spock的第二个堆栈跟踪直接 Thread [main](Suspended(brea ) ArtifactManagerSpecification。$ spock_feature_0_0()line:65 NativeMethodAccessorImpl.invoke0(方法,对象,对象[])行:不可用[本机方法] NativeMethodAccessorImpl .invoke(Object,Object [])行:57 DelegatingMethodAccessorImpl.invoke(Object,Object [])行:43 Method.invoke(Object,Object ...)行:601 行ReflectionUtil.invokeMethod(Object,Method,Object ...)行: ParameterizedSpecRunner(BaseSpecRunner).invokeRaw(Object,MethodInfo,Object ...)行:330 ParameterizedSpecRunner(BaseSpecRunner).invoke (Object,MethodInfo,Object ...)行:311 ParameterizedSpecRunner(BaseSpecRunner).invokeFeatureMethod()行:285 ParameterizedSpecRunner(BaseSpecRunner).doRunIteration()行:256 NativeMethodAccessorImpl.invoke0 (Method,Object,Object [])line:不可用[native method] NativeMethodAccessorImpl.invoke(Object,Object [])行:57 DelegateMethodAccessorImpl.invoke(Object,Object [])行:43 Method.invoke(Object,Object ...)行:601 ReflectionUtil.invokeMethod(Object,Method,Object ...)行: 138 ParameterizedSpecRunner(BaseSpecRunner).invokeRaw(Object,MethodInfo,Object ...)行:330 ParameterizedSpecRunner(BaseSpecRunner).invoke(Object,MethodInfo,Object ...)行:311 ParameterizedSpecRunner(BaseSpecRunner).runIteration(Object [],int)行:223 ParameterizedSpecRunner(BaseSpecRunner).initializeAndRunIteration(Object [],int)行:214 ParameterizedSpecRunner(BaseSpecRunner).runSimpleFeature :205 ParameterizedSpecRunner(BaseSpecRunner).doRunFeature()行:199 NativeMethodAccessorImpl.invoke0(方法,对象,对象[])行:不可用[本机方法] NativeMethodAccessorImpl.invoke(Object ,Object [])行:57 DelegatingMethodAccessorImpl.invoke(Object,Object [])行:43 Method.invoke(Object,Object ...)行:6 01 ReflectionUtil.invokeMethod(Object,Method,Object ...)行:138 ParameterizedSpecRunner(BaseSpecRunner).invokeRaw(Object,MethodInfo,Object ...)行:330 ParameterizedSpecRunner BaseSpecRunner).invoke(Object,MethodInfo,Object ...)行:311 ParameterizedSpecRunner(BaseSpecRunner).runFeature()行:175 ParameterizedSpecRunner(BaseSpecRunner).runFeatures()行:152 参数化的SpecRunner(BaseSpecRunner).doRunSpec()行:112 NativeMethodAccessorImpl.invoke0(方法,对象,对象[])行:不可用[本机方法] NativeMethodAccessorImpl.invoke(Object,Object [])行:57 DelegatingMethodAccessorImpl.invoke(Object,Object [])行:43 Method.invoke(Object,Object ...)行:601 ReflectionUtil.invokeMethod(Object,Method,对象...)行:138 ParameterizedSpecRunner(BaseSpecRunner).invokeRaw(Object,MethodInfo,Object ...)line:330 ParameterizedSpecRunner(BaseSpecRunner).invoke(Object, MethodInfo,Object ...)行:311 ParameterizedSpecRunner(BaseSpecRunner).runSpec()行:91 ParameterizedSpecRunner(BaseSpecRunner).run()行:82 Sputnik.run(RunNotifier)行:63 JUnit4TestClassReference(JUnit4TestReference).run(TestExecution)行:50 TestExecution.run(ITestReference [])行:38 RemoteTestRunner.runTests(String [],String,TestExecution) line:467 RemoteTestRunner.runTests(TestExecution)行:683 RemoteTestRunner.run()行:390 RemoteTestRunner.main(String [])行:197 解决方案我进一步调查了这一点,至少我找到了原因。 $ b 在下面的 org.testng.junit.JUnit4TestMethod 中,Spock测试名称无法正确解析。我猜测,但我认为Spock使用一种名称来支持它允许的漂亮的文本方法名称。因此,尽管下面的TestNG代码得到了一个像This is my test这样的方法名,但它不能通过显示的方式来解决这个问题。我相信真正的方法名称是这样的:$ spb_feature_0_0 私有静态方法getMethod(描述desc){类< / p> ;?> c = desc.getTestClass(); String method = desc.getMethodName(); 尝试{ return c.getMethod(method); } catch(Throwable t){ Utils.log(JUnit4TestMethod,2,Method'+ method +'class not found in class'+ c.getName() +':+ t.getMessage()); 返回null; $ b 因此,这会在通话中引发异常 c.getMethod(method) - 它无法将Spock方法名称解析为类上的实际方法。 看起来真的'解决这个问题'是上面的代码在这段代码中被Junit( org.junit.runner.notification.RunNotifier )调用: 同步(fListeners){> 私有抽象类SafeNotifier { ); all.hasNext();) try { notifyListener(all.next()); } catch(Exception e){ all.remove(); //首先删除违规的监听器,以避免无限循环 fireTestFailure(new Failure(Description.TEST_MECHANISM,e)); } } } 这段代码不会悄悄地删除监听器,而是将其记录下来...... Need to get TestNG to run my Spock Tests as TestNG is used throughout the rest of the system.Since TestNG supports running JUnit tests, I tried this:<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" ><suite name="suiteTestName" verbose="1" junit="true"> <test name="myTestName"> <classes> <class name="mypackage.artifact.SomeArtifactSpecification" /> </classes> </test></suite>So this actually ran the Groovy 'SomeArtifactSpecification' Spock Unit Test perfectly. I could see some I/O from the tests, but since I ran from within Eclipse I used the debugger to verify that all the tests were executing properly. My classpath has TestNG-6.8.7 and JUnit-4.10 on it. The problem is that TestNG doesn't seem to know that it did! Here is the end output:===============================================myTestNameTotal tests run: 0, Failures: 0, Skips: 0===============================================Does anyone know if it is possible to get TestNG to recognize these tests?Additional Details - added for the BountyIn one of the comments below, Peter states that it depends upon what TestNG is doing. I investigated that as much as I could. It seems like both Junit and TestNG invoke the 'run' method of Spock's Sputnik class line 63. However, when invoked directly from JUnit, Sputnik is called from JUnit4TestClassReference. When called from TestNG, Sputnik is invoked from JUnitCore.So the question remains: How to get TestNG to recognize the test outputs from Spock? If I am to live long and prosper, I really need to know!!! It seems like the answer may be to get TestNG to run with JUnit4TestClassReference, or to use some type of clever wrapper to make TestNG do that...For the gory details, I posted two stack traces below. The first is from the TestNG run, which does not work, the second is from running Spock directly without TestNG.First Stack Trace from TestNG:org.testng.remote.RemoteTestNG at localhost:54288Thread [main] (Suspended (breakpoint at line 65 in ArtifactManagerSpecification)) ArtifactManagerSpecification.$spock_feature_0_0() line: 65 NativeMethodAccessorImpl.invoke0(Method, Object, Object[]) line: not available [native method] NativeMethodAccessorImpl.invoke(Object, Object[]) line: 57 DelegatingMethodAccessorImpl.invoke(Object, Object[]) line: 43 Method.invoke(Object, Object...) line: 601 ReflectionUtil.invokeMethod(Object, Method, Object...) line: 138 ParameterizedSpecRunner(BaseSpecRunner).invokeRaw(Object, MethodInfo, Object...) line: 330 ParameterizedSpecRunner(BaseSpecRunner).invoke(Object, MethodInfo, Object...) line: 311 ParameterizedSpecRunner(BaseSpecRunner).invokeFeatureMethod() line: 285 ParameterizedSpecRunner(BaseSpecRunner).doRunIteration() line: 256 NativeMethodAccessorImpl.invoke0(Method, Object, Object[]) line: not available [native method] NativeMethodAccessorImpl.invoke(Object, Object[]) line: 57 DelegatingMethodAccessorImpl.invoke(Object, Object[]) line: 43 Method.invoke(Object, Object...) line: 601 ReflectionUtil.invokeMethod(Object, Method, Object...) line: 138 ParameterizedSpecRunner(BaseSpecRunner).invokeRaw(Object, MethodInfo, Object...) line: 330 ParameterizedSpecRunner(BaseSpecRunner).invoke(Object, MethodInfo, Object...) line: 311 ParameterizedSpecRunner(BaseSpecRunner).runIteration(Object[], int) line: 223 ParameterizedSpecRunner(BaseSpecRunner).initializeAndRunIteration(Object[], int) line: 214 ParameterizedSpecRunner(BaseSpecRunner).runSimpleFeature() line: 205 ParameterizedSpecRunner(BaseSpecRunner).doRunFeature() line: 199 NativeMethodAccessorImpl.invoke0(Method, Object, Object[]) line: not available [native method] NativeMethodAccessorImpl.invoke(Object, Object[]) line: 57 DelegatingMethodAccessorImpl.invoke(Object, Object[]) line: 43 Method.invoke(Object, Object...) line: 601 ReflectionUtil.invokeMethod(Object, Method, Object...) line: 138 ParameterizedSpecRunner(BaseSpecRunner).invokeRaw(Object, MethodInfo, Object...) line: 330 ParameterizedSpecRunner(BaseSpecRunner).invoke(Object, MethodInfo, Object...) line: 311 ParameterizedSpecRunner(BaseSpecRunner).runFeature() line: 175 ParameterizedSpecRunner(BaseSpecRunner).runFeatures() line: 152 ParameterizedSpecRunner(BaseSpecRunner).doRunSpec() line: 112 NativeMethodAccessorImpl.invoke0(Method, Object, Object[]) line: not available [native method] NativeMethodAccessorImpl.invoke(Object, Object[]) line: 57 DelegatingMethodAccessorImpl.invoke(Object, Object[]) line: 43 Method.invoke(Object, Object...) line: 601 ReflectionUtil.invokeMethod(Object, Method, Object...) line: 138 ParameterizedSpecRunner(BaseSpecRunner).invokeRaw(Object, MethodInfo, Object...) line: 330 ParameterizedSpecRunner(BaseSpecRunner).invoke(Object, MethodInfo, Object...) line: 311 ParameterizedSpecRunner(BaseSpecRunner).runSpec() line: 91 ParameterizedSpecRunner(BaseSpecRunner).run() line: 82 Sputnik.run(RunNotifier) line: 63 JUnitCore.run(Runner) line: 157 JUnitCore.run(Request) line: 136 JUnit4TestRunner.start(Class, String...) line: 81 JUnit4TestRunner.run(Class, String...) line: 69 TestRunner$1.run() line: 682 TestRunner.runWorkers(List<IWorker<ITestNGMethod>>, String, ListMultiMap<Integer,TestMethodWorker>) line: 1005 TestRunner.privateRunJUnit(XmlTest) line: 713 TestRunner.run() line: 614 SuiteRunner.runTest(TestRunner) line: 334 SuiteRunner.runSequentially() line: 329 SuiteRunner.privateRun() line: 291 SuiteRunner.run() line: 240 SuiteRunnerWorker.runSuite(SuiteRunnerMap, XmlSuite) line: 52 SuiteRunnerWorker.run() line: 86 RemoteTestNG(TestNG).runSuitesSequentially(XmlSuite, SuiteRunnerMap, int, String) line: 1224 RemoteTestNG(TestNG).runSuitesLocally() line: 1149 RemoteTestNG(TestNG).run() line: 1057 RemoteTestNG.run() line: 111 RemoteTestNG.initAndRun(String[], CommandLineArgs, RemoteArgs) line: 204 RemoteTestNG.main(String[]) line: 175Second Stack Trace (that works) running Spock directlyThread [main] (Suspended (breakpoint at line 65 in ArtifactManagerSpecification))ArtifactManagerSpecification.$spock_feature_0_0() line: 65NativeMethodAccessorImpl.invoke0(Method, Object, Object[]) line: not available [native method]NativeMethodAccessorImpl.invoke(Object, Object[]) line: 57DelegatingMethodAccessorImpl.invoke(Object, Object[]) line: 43Method.invoke(Object, Object...) line: 601ReflectionUtil.invokeMethod(Object, Method, Object...) line: 138ParameterizedSpecRunner(BaseSpecRunner).invokeRaw(Object, MethodInfo, Object...) line: 330ParameterizedSpecRunner(BaseSpecRunner).invoke(Object, MethodInfo, Object...) line: 311ParameterizedSpecRunner(BaseSpecRunner).invokeFeatureMethod() line: 285ParameterizedSpecRunner(BaseSpecRunner).doRunIteration() line: 256NativeMethodAccessorImpl.invoke0(Method, Object, Object[]) line: not available [native method]NativeMethodAccessorImpl.invoke(Object, Object[]) line: 57DelegatingMethodAccessorImpl.invoke(Object, Object[]) line: 43Method.invoke(Object, Object...) line: 601ReflectionUtil.invokeMethod(Object, Method, Object...) line: 138ParameterizedSpecRunner(BaseSpecRunner).invokeRaw(Object, MethodInfo, Object...) line: 330ParameterizedSpecRunner(BaseSpecRunner).invoke(Object, MethodInfo, Object...) line: 311ParameterizedSpecRunner(BaseSpecRunner).runIteration(Object[], int) line: 223ParameterizedSpecRunner(BaseSpecRunner).initializeAndRunIteration(Object[], int) line: 214ParameterizedSpecRunner(BaseSpecRunner).runSimpleFeature() line: 205ParameterizedSpecRunner(BaseSpecRunner).doRunFeature() line: 199NativeMethodAccessorImpl.invoke0(Method, Object, Object[]) line: not available [native method]NativeMethodAccessorImpl.invoke(Object, Object[]) line: 57DelegatingMethodAccessorImpl.invoke(Object, Object[]) line: 43Method.invoke(Object, Object...) line: 601ReflectionUtil.invokeMethod(Object, Method, Object...) line: 138ParameterizedSpecRunner(BaseSpecRunner).invokeRaw(Object, MethodInfo, Object...) line: 330ParameterizedSpecRunner(BaseSpecRunner).invoke(Object, MethodInfo, Object...) line: 311ParameterizedSpecRunner(BaseSpecRunner).runFeature() line: 175ParameterizedSpecRunner(BaseSpecRunner).runFeatures() line: 152ParameterizedSpecRunner(BaseSpecRunner).doRunSpec() line: 112NativeMethodAccessorImpl.invoke0(Method, Object, Object[]) line: not available [native method]NativeMethodAccessorImpl.invoke(Object, Object[]) line: 57DelegatingMethodAccessorImpl.invoke(Object, Object[]) line: 43Method.invoke(Object, Object...) line: 601ReflectionUtil.invokeMethod(Object, Method, Object...) line: 138ParameterizedSpecRunner(BaseSpecRunner).invokeRaw(Object, MethodInfo, Object...) line: 330ParameterizedSpecRunner(BaseSpecRunner).invoke(Object, MethodInfo, Object...) line: 311ParameterizedSpecRunner(BaseSpecRunner).runSpec() line: 91ParameterizedSpecRunner(BaseSpecRunner).run() line: 82Sputnik.run(RunNotifier) line: 63JUnit4TestClassReference(JUnit4TestReference).run(TestExecution) line: 50TestExecution.run(ITestReference[]) line: 38RemoteTestRunner.runTests(String[], String, TestExecution) line: 467RemoteTestRunner.runTests(TestExecution) line: 683RemoteTestRunner.run() line: 390RemoteTestRunner.main(String[]) line: 197 解决方案 I investigated this further and at least I found the cause.In org.testng.junit.JUnit4TestMethod below, Spock test names are unable to be properly resolved. I am guessing, but I think Spock uses a type of name mangling to support the nice text method name it allows. Thus, even though the TestNG code below gets a method name like "This is my test", it can not resolve that via the means shown. I believe the real method name is something like: $spock_feature_0_0private static Method getMethod(Description desc) { Class<?> c = desc.getTestClass(); String method = desc.getMethodName(); try { return c.getMethod(method); } catch (Throwable t) { Utils.log("JUnit4TestMethod", 2, "Method '" + method + "' not found in class '" + c.getName() + "': " + t.getMessage()); return null; }}As a result, this throws an exception on the call c.getMethod(method) - it can't resolve the Spock method name to an actual method on the class.What seems to really 'muck up the situation' is that the code above is called by Junit (org.junit.runner.notification.RunNotifier) in this code:private abstract class SafeNotifier { void run() { synchronized (fListeners) { for (Iterator<RunListener> all= fListeners.iterator(); all.hasNext();) try { notifyListener(all.next()); } catch (Exception e) { all.remove(); // Remove the offending listener first to avoid an infinite loop fireTestFailure(new Failure(Description.TEST_MECHANISM, e)); } } }it would be really nice for this code to not quietly remove the listener, but rather log it... 这篇关于TestNG可以看到我的Spock(JUnit)测试结果吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
07-31 13:03