要使用Evosuite为Java项目生成测试时,必须指定:
正在测试的类
可在其中找到被测类的字节码及其依赖项的类路径
所以我在命令提示符下给出了以下内容:
$EVOSUITE -prefix com.lightbend.akka.sample -projectCP target/classes:target/dependency
$EVOSUITE
代表java -jar evosuite.jar
com.lightbend.akka.sample
代表了我有四个要测试的类的程序包。
target/classes
是我所有的类都处于测试字节码下的文件夹。
target/dependency
是我依赖于jar的文件夹。
在测试的四个类中,一个没有依赖性。这是堆栈弹出和推入的简单实现。如果我在com.lightbend.akka.sample
内只有这个课程,那么Evosuite会自动为该课程生成测试套件。
其他三个类是使用AKKA的实现,因此与target/dependency
中的其他jar中的Akka jar有关。对于这些测试,由于Evosuite无法找到类,所以生成失败:Error while initializing target class: Class not found: akka/actor/ActorRef:
我在做什么错?您如何使用Evosuite为具有外部依赖项的多个类生成测试?
下面是在上面发出我的命令后出现的完整错误:
* EvoSuite 1.0.6
* Analyzing classpath (generating inheritance tree)
- target/classes
- target/dependency
* Found 4 matching classes for prefix com.lightbend.akka.sample
* Current class: com.lightbend.akka.sample.Printer
* Going to generate test cases for class: com.lightbend.akka.sample.Printer
* Starting client
* Connecting to master process on port 7154
* Analyzing classpath:
* Error while initializing target class: akka/actor/AbstractActor
[MASTER] 12:11:18.052 [logback-2] ERROR TestSuiteGenerator - Problem for com.lightbend.akka.sample.Printer. Full stack:
java.lang.ClassNotFoundException: akka/actor/AbstractActor
at org.evosuite.instrumentation.InstrumentingClassLoader.instrumentClass(InstrumentingClassLoader.java:201) ~[evosuite-1.0.6.jar:1.0.6]
at org.evosuite.instrumentation.InstrumentingClassLoader.loadClass(InstrumentingClassLoader.java:168) ~[evosuite-1.0.6.jar:1.0.6]
at java.lang.Class.forName0(Native Method) ~[na:1.8.0_181]
at java.lang.Class.forName(Class.java:348) ~[na:1.8.0_181]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_181]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_181]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_181]
at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_181]
at org.evosuite.testcase.statements.MethodStatement$1.execute(MethodStatement.java:257) ~[evosuite-1.0.6.jar:1.0.6]
at org.evosuite.testcase.statements.AbstractStatement.exceptionHandler(AbstractStatement.java:169) ~[evosuite-1.0.6.jar:1.0.6]
at org.evosuite.testcase.statements.MethodStatement.execute(MethodStatement.java:220) ~[evosuite-1.0.6.jar:1.0.6]
at org.evosuite.testcase.execution.TestRunnable.executeStatements(TestRunnable.java:307) ~[evosuite-1.0.6.jar:1.0.6]
at org.evosuite.testcase.execution.TestRunnable.call(TestRunnable.java:213) ~[evosuite-1.0.6.jar:1.0.6]
at org.evosuite.testcase.execution.TestRunnable.call(TestRunnable.java:55) ~[evosuite-1.0.6.jar:1.0.6]
at java.util.concurrent.FutureTask.run(FutureTask.java:266) [na:1.8.0_181]
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) [na:1.8.0_181]
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) [na:1.8.0_181]
at java.lang.Thread.run(Thread.java:748) [na:1.8.0_181]
Caused by: java.lang.NoClassDefFoundError: akka/actor/AbstractActor
at java.lang.ClassLoader.defineClass1(Native Method) ~[na:1.8.0_181]
at java.lang.ClassLoader.defineClass(ClassLoader.java:763) ~[na:1.8.0_181]
at java.lang.ClassLoader.defineClass(ClassLoader.java:642) ~[na:1.8.0_181]
at org.evosuite.instrumentation.InstrumentingClassLoader.instrumentClass(InstrumentingClassLoader.java:194) ~[evosuite-1.0.6.jar:1.0.6]
... 17 common frames omitted
Caused by: java.lang.ClassNotFoundException: Class 'akka/actor/AbstractActor.class' should be in target project, but could not be found!
at org.evosuite.instrumentation.InstrumentingClassLoader.instrumentClass(InstrumentingClassLoader.java:201) ~[evosuite-1.0.6.jar:1.0.6]
at org.evosuite.instrumentation.InstrumentingClassLoader.loadClass(InstrumentingClassLoader.java:168) ~[evosuite-1.0.6.jar:1.0.6]
... 21 common frames omitted
Caused by: java.lang.ClassNotFoundException: Class 'akka/actor/AbstractActor.class' should be in target project, but could not be found!
at org.evosuite.instrumentation.InstrumentingClassLoader.instrumentClass(InstrumentingClassLoader.java:188) ~[evosuite-1.0.6.jar:1.0.6]
... 22 common frames omitted
* Computation finished
[MASTER] 12:11:18.181 [main] ERROR SearchStatistics - No statistics has been saved because EvoSuite failed to generate any test case
[MASTER] 12:11:18.282 [main] ERROR TestGeneration - failed to write statistics data
* Current class: com.lightbend.akka.sample.Stack
* Going to generate test cases for class: com.lightbend.akka.sample.Stack
* Starting client
* Connecting to master process on port 3398
* Analyzing classpath:
* Inheritance tree loaded from /tmp/ES_inheritancetree5527612279643905395.xml.gz
* Finished analyzing classpath
* Generating tests for class com.lightbend.akka.sample.Stack
* Test criteria:
- Line Coverage
- Branch Coverage
- Exception
- Mutation testing (weak)
- Method-Output Coverage
- Top-Level Method Coverage
- No-Exception Top-Level Method Coverage
- Context Branch Coverage
* Setting up search algorithm for whole suite generation
* Total number of test goals:
- Line 12
- Branch 7
- Exception 0
- MutationFactory 40
- Output 4
- Method 4
- MethodNoException 4
- CBranchFitnessFactory 7
* Using seed 1539252678603
* Starting evolution
[Progress:==============================100%] [Cov:==================================>99%]
* Search finished after 61s and 1668 generations, 404567 statements, best individual has fitness: 1.8333333333333333
* Minimizing test suite
* Going to analyze the coverage criteria
* Coverage analysis for criterion LINE
* Coverage of criterion LINE: 100%
* Total number of goals: 12
* Number of covered goals: 12
* Coverage analysis for criterion BRANCH
* Coverage of criterion BRANCH: 100%
* Total number of goals: 7
* Number of covered goals: 7
* Coverage analysis for criterion EXCEPTION
* Coverage of criterion EXCEPTION: 100%
* Total number of goals: 2
* Number of covered goals: 2
* Coverage analysis for criterion WEAKMUTATION
* Coverage of criterion WEAKMUTATION: 92%
* Total number of goals: 40
* Number of covered goals: 37
* Coverage analysis for criterion OUTPUT
* Coverage of criterion OUTPUT: 100%
* Total number of goals: 4
* Number of covered goals: 4
* Coverage analysis for criterion METHOD
* Coverage of criterion METHOD: 100%
* Total number of goals: 4
* Number of covered goals: 4
* Coverage analysis for criterion METHODNOEXCEPTION
* Coverage of criterion METHODNOEXCEPTION: 100%
* Total number of goals: 4
* Number of covered goals: 4
* Coverage analysis for criterion CBRANCH
* Coverage of criterion CBRANCH: 100%
* Total number of goals: 7
* Number of covered goals: 7
* Generated 6 tests with total length 26
* Resulting test suite's coverage: 99% (average coverage for all fitness functions)
* Generating assertions
* Resulting test suite's mutation score: 67%
* Compiling and checking tests
* Writing JUnit test case 'Stack_ESTest' to evosuite-tests
* Done!
* Computation finished
* Current class: com.lightbend.akka.sample.Greeter
* Going to generate test cases for class: com.lightbend.akka.sample.Greeter
* Starting client
* Connecting to master process on port 7164
* Analyzing classpath:
* Error while initializing target class: akka/actor/AbstractActor
[MASTER] 12:12:23.277 [logback-2] ERROR TestSuiteGenerator - Problem for com.lightbend.akka.sample.Greeter. Full stack:
java.lang.ClassNotFoundException: akka/actor/AbstractActor
at org.evosuite.instrumentation.InstrumentingClassLoader.instrumentClass(InstrumentingClassLoader.java:201) ~[evosuite-1.0.6.jar:1.0.6]
at org.evosuite.instrumentation.InstrumentingClassLoader.loadClass(InstrumentingClassLoader.java:168) ~[evosuite-1.0.6.jar:1.0.6]
at java.lang.Class.forName0(Native Method) ~[na:1.8.0_181]
at java.lang.Class.forName(Class.java:348) ~[na:1.8.0_181]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_181]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_181]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_181]
at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_181]
at org.evosuite.testcase.statements.MethodStatement$1.execute(MethodStatement.java:257) ~[evosuite-1.0.6.jar:1.0.6]
at org.evosuite.testcase.statements.AbstractStatement.exceptionHandler(AbstractStatement.java:169) ~[evosuite-1.0.6.jar:1.0.6]
at org.evosuite.testcase.statements.MethodStatement.execute(MethodStatement.java:220) ~[evosuite-1.0.6.jar:1.0.6]
at org.evosuite.testcase.execution.TestRunnable.executeStatements(TestRunnable.java:307) ~[evosuite-1.0.6.jar:1.0.6]
at org.evosuite.testcase.execution.TestRunnable.call(TestRunnable.java:213) ~[evosuite-1.0.6.jar:1.0.6]
at org.evosuite.testcase.execution.TestRunnable.call(TestRunnable.java:55) ~[evosuite-1.0.6.jar:1.0.6]
at java.util.concurrent.FutureTask.run(FutureTask.java:266) [na:1.8.0_181]
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) [na:1.8.0_181]
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) [na:1.8.0_181]
at java.lang.Thread.run(Thread.java:748) [na:1.8.0_181]
Caused by: java.lang.NoClassDefFoundError: akka/actor/AbstractActor
at java.lang.ClassLoader.defineClass1(Native Method) ~[na:1.8.0_181]
at java.lang.ClassLoader.defineClass(ClassLoader.java:763) ~[na:1.8.0_181]
at java.lang.ClassLoader.defineClass(ClassLoader.java:642) ~[na:1.8.0_181]
at org.evosuite.instrumentation.InstrumentingClassLoader.instrumentClass(InstrumentingClassLoader.java:194) ~[evosuite-1.0.6.jar:1.0.6]
... 17 common frames omitted
Caused by: java.lang.ClassNotFoundException: Class 'akka/actor/AbstractActor.class' should be in target project, but could not be found!
at org.evosuite.instrumentation.InstrumentingClassLoader.instrumentClass(InstrumentingClassLoader.java:201) ~[evosuite-1.0.6.jar:1.0.6]
at org.evosuite.instrumentation.InstrumentingClassLoader.loadClass(InstrumentingClassLoader.java:168) ~[evosuite-1.0.6.jar:1.0.6]
... 21 common frames omitted
Caused by: java.lang.ClassNotFoundException: Class 'akka/actor/AbstractActor.class' should be in target project, but could not be found!
at org.evosuite.instrumentation.InstrumentingClassLoader.instrumentClass(InstrumentingClassLoader.java:188) ~[evosuite-1.0.6.jar:1.0.6]
... 22 common frames omitted
* Computation finished
[MASTER] 12:12:23.386 [main] ERROR SearchStatistics - No statistics has been saved because EvoSuite failed to generate any test case
[MASTER] 12:12:23.487 [main] ERROR TestGeneration - failed to write statistics data
* Current class: com.lightbend.akka.sample.AkkaQuickstart
* Going to generate test cases for class: com.lightbend.akka.sample.AkkaQuickstart
* Starting client
* Connecting to master process on port 13590
* Analyzing classpath:
* Error while initializing target class: Class not found: akka/actor/ActorRef: java.lang.NullPointerException: Class not found akka/actor/ActorRef
[MASTER] 12:12:24.206 [logback-2] ERROR TestSuiteGenerator - Problem for com.lightbend.akka.sample.AkkaQuickstart. Full stack:
java.lang.ClassNotFoundException: Class not found: akka/actor/ActorRef: java.lang.NullPointerException: Class not found akka/actor/ActorRef
at org.evosuite.instrumentation.InstrumentingClassLoader.instrumentClass(InstrumentingClassLoader.java:201) ~[evosuite-1.0.6.jar:1.0.6]
at org.evosuite.instrumentation.InstrumentingClassLoader.loadClass(InstrumentingClassLoader.java:168) ~[evosuite-1.0.6.jar:1.0.6]
at java.lang.Class.forName0(Native Method) ~[na:1.8.0_181]
at java.lang.Class.forName(Class.java:348) ~[na:1.8.0_181]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_181]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_181]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_181]
at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_181]
at org.evosuite.testcase.statements.MethodStatement$1.execute(MethodStatement.java:257) ~[evosuite-1.0.6.jar:1.0.6]
at org.evosuite.testcase.statements.AbstractStatement.exceptionHandler(AbstractStatement.java:169) ~[evosuite-1.0.6.jar:1.0.6]
at org.evosuite.testcase.statements.MethodStatement.execute(MethodStatement.java:220) ~[evosuite-1.0.6.jar:1.0.6]
at org.evosuite.testcase.execution.TestRunnable.executeStatements(TestRunnable.java:307) ~[evosuite-1.0.6.jar:1.0.6]
at org.evosuite.testcase.execution.TestRunnable.call(TestRunnable.java:213) ~[evosuite-1.0.6.jar:1.0.6]
at org.evosuite.testcase.execution.TestRunnable.call(TestRunnable.java:55) ~[evosuite-1.0.6.jar:1.0.6]
at java.util.concurrent.FutureTask.run(FutureTask.java:266) [na:1.8.0_181]
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) [na:1.8.0_181]
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) [na:1.8.0_181]
at java.lang.Thread.run(Thread.java:748) [na:1.8.0_181]
Caused by: java.lang.RuntimeException: Class not found: akka/actor/ActorRef: java.lang.NullPointerException: Class not found akka/actor/ActorRef
at org.evosuite.runtime.util.ComputeClassWriter.getCommonSuperClass(ComputeClassWriter.java:58) ~[evosuite-1.0.6.jar:1.0.6]
at org.evosuite.shaded.org.objectweb.asm.ClassWriter.getMergedType(ClassWriter.java:1729) ~[evosuite-1.0.6.jar:1.0.6]
at org.evosuite.shaded.org.objectweb.asm.Frame.merge(Frame.java:1530) ~[evosuite-1.0.6.jar:1.0.6]
at org.evosuite.shaded.org.objectweb.asm.Frame.merge(Frame.java:1429) ~[evosuite-1.0.6.jar:1.0.6]
at org.evosuite.shaded.org.objectweb.asm.MethodWriter.visitMaxs(MethodWriter.java:1497) ~[evosuite-1.0.6.jar:1.0.6]
at org.evosuite.shaded.org.objectweb.asm.MethodVisitor.visitMaxs(MethodVisitor.java:867) ~[evosuite-1.0.6.jar:1.0.6]
at org.evosuite.shaded.org.objectweb.asm.MethodVisitor.visitMaxs(MethodVisitor.java:867) ~[evosuite-1.0.6.jar:1.0.6]
at org.evosuite.shaded.org.objectweb.asm.MethodVisitor.visitMaxs(MethodVisitor.java:867) ~[evosuite-1.0.6.jar:1.0.6]
at org.evosuite.runtime.instrumentation.LoopCounterMethodAdapter.visitMaxs(LoopCounterMethodAdapter.java:48) ~[evosuite-1.0.6.jar:1.0.6]
at org.evosuite.shaded.org.objectweb.asm.MethodVisitor.visitMaxs(MethodVisitor.java:867) ~[evosuite-1.0.6.jar:1.0.6]
at org.evosuite.shaded.org.objectweb.asm.commons.LocalVariablesSorter.visitMaxs(LocalVariablesSorter.java:165) ~[evosuite-1.0.6.jar:1.0.6]
at org.evosuite.instrumentation.MethodEntryAdapter.visitMaxs(MethodEntryAdapter.java:109) ~[evosuite-1.0.6.jar:1.0.6]
at org.evosuite.shaded.org.objectweb.asm.MethodVisitor.visitMaxs(MethodVisitor.java:867) ~[evosuite-1.0.6.jar:1.0.6]
at org.evosuite.instrumentation.LineNumberMethodAdapter.visitMaxs(LineNumberMethodAdapter.java:127) ~[evosuite-1.0.6.jar:1.0.6]
at org.evosuite.shaded.org.objectweb.asm.commons.LocalVariablesSorter.visitMaxs(LocalVariablesSorter.java:165) ~[evosuite-1.0.6.jar:1.0.6]
at org.evosuite.shaded.org.objectweb.asm.MethodVisitor.visitMaxs(MethodVisitor.java:867) ~[evosuite-1.0.6.jar:1.0.6]
at org.evosuite.shaded.org.objectweb.asm.tree.MethodNode.accept(MethodNode.java:835) ~[evosuite-1.0.6.jar:1.0.6]
at org.evosuite.shaded.org.objectweb.asm.commons.JSRInlinerAdapter.visitEnd(JSRInlinerAdapter.java:187) ~[evosuite-1.0.6.jar:1.0.6]
at org.evosuite.shaded.org.objectweb.asm.tree.MethodNode.accept(MethodNode.java:838) ~[evosuite-1.0.6.jar:1.0.6]
at org.evosuite.graphs.cfg.CFGMethodAdapter.visitEnd(CFGMethodAdapter.java:263) ~[evosuite-1.0.6.jar:1.0.6]
at org.evosuite.shaded.org.objectweb.asm.MethodVisitor.visitEnd(MethodVisitor.java:878) ~[evosuite-1.0.6.jar:1.0.6]
at org.evosuite.shaded.org.objectweb.asm.MethodVisitor.visitEnd(MethodVisitor.java:878) ~[evosuite-1.0.6.jar:1.0.6]
at org.evosuite.shaded.org.objectweb.asm.tree.MethodNode.accept(MethodNode.java:838) ~[evosuite-1.0.6.jar:1.0.6]
at org.evosuite.shaded.org.objectweb.asm.tree.MethodNode.accept(MethodNode.java:727) ~[evosuite-1.0.6.jar:1.0.6]
at org.evosuite.shaded.org.objectweb.asm.tree.ClassNode.accept(ClassNode.java:452) ~[evosuite-1.0.6.jar:1.0.6]
at org.evosuite.instrumentation.BytecodeInstrumentation.transformBytes(BytecodeInstrumentation.java:287) ~[evosuite-1.0.6.jar:1.0.6]
at org.evosuite.instrumentation.InstrumentingClassLoader.getTransformedBytes(InstrumentingClassLoader.java:175) ~[evosuite-1.0.6.jar:1.0.6]
at org.evosuite.instrumentation.InstrumentingClassLoader.instrumentClass(InstrumentingClassLoader.java:192) ~[evosuite-1.0.6.jar:1.0.6]
... 17 common frames omitted
Caused by: java.lang.NullPointerException: Class not found akka/actor/ActorRef
at org.evosuite.runtime.util.ComputeClassWriter.typeInfo(ComputeClassWriter.java:186) ~[evosuite-1.0.6.jar:1.0.6]
at org.evosuite.runtime.util.ComputeClassWriter.getCommonSuperClass(ComputeClassWriter.java:55) ~[evosuite-1.0.6.jar:1.0.6]
... 44 common frames omitted
* Computation finished
[MASTER] 12:12:24.320 [main] ERROR SearchStatistics - No statistics has been saved because EvoSuite failed to generate any test case
[MASTER] 12:12:24.421 [main] ERROR TestGeneration - failed to write statistics data