EvoSuite / evosuite

EvoSuite - automated generation of JUnit test suites for Java classes
http://www.evosuite.org
GNU Lesser General Public License v3.0
832 stars 341 forks source link

Unable to create test class . Exception raised java.lang.NoClassDefFoundError: org/apache/logging/log4j/LogManager #365

Closed RSoodAMAT closed 3 years ago

RSoodAMAT commented 3 years ago

Context

We have created a spring boot jdk project. We are using log4j library to record logs. While generating test case for the class , exception is being raised .

It is a gradle project .

Steps to Reproduce

Please break down here below all the needed steps to reproduce the issue. [If possible, please upload an example of the project you are generating tests for.]

  1. Create a spring boot , java gradle project
  2. Create a controller class . Also add looging using log4j library.
  3. Build the project
  4. Execute below command
    $EVOSUITE -class com.poc.controller.RecipeController -projectCP build/classes/java/main

EvoSuite Arguments

$EVOSUITE -class com.poc.controller.RecipeController -projectCP build/classes/java/main

Current Result

$EVOSUITE -class com.poc.controller.RecipeController -projectCP build/classes/java/main

  • EvoSuite 1.1.0
  • Going to generate test cases for class: com.poc.controller.RecipeController
  • Starting Client-0
  • Connecting to master process on port 19620
  • Analyzing classpath:
  • build/classes/java/main
  • Error while initializing target class: org/apache/logging/log4j/LogManager
  • Computation finished [MASTER] 15:40:30.893 [logback-2] ERROR TestSuiteGenerator - Problem for com.poc.controller.RecipeController. Full stack: java.lang.NoClassDefFoundError: org/apache/logging/log4j/LogManager at com.poc.controller.RecipeController.(RecipeController.java) at java.base/java.lang.Class.forName0(Native Method) at java.base/java.lang.Class.forName(Class.java:398) at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.base/java.lang.reflect.Method.invoke(Method.java:566) at org.evosuite.testcase.statements.MethodStatement$1.execute(MethodStatement.java:261) at org.evosuite.testcase.statements.AbstractStatement.exceptionHandler(AbstractStatement.java:172) at org.evosuite.testcase.statements.MethodStatement.execute(MethodStatement.java:224) at org.evosuite.testcase.execution.TestRunnable.executeStatements(TestRunnable.java:295) at org.evosuite.testcase.execution.TestRunnable.call(TestRunnable.java:201) at org.evosuite.testcase.execution.TestRunnable.call(TestRunnable.java:54) at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264) at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128) at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628) at java.base/java.lang.Thread.run(Thread.java:834) Caused by: java.lang.ClassNotFoundException: Class 'org/apache/logging/log4j/LogManager.class' should be in target project, but could not be found! at org.evosuite.instrumentation.InstrumentingClassLoader.instrumentClass(InstrumentingClassLoader.java:164) at org.evosuite.instrumentation.InstrumentingClassLoader.loadClass(InstrumentingClassLoader.java:132) ... 17 common frames omitted Caused by: java.lang.ClassNotFoundException: Class 'org/apache/logging/log4j/LogManager.class' should be in target project, but could not be found! at org.evosuite.instrumentation.InstrumentingClassLoader.instrumentClass(InstrumentingClassLoader.java:151) ... 18 common frames omitted [MASTER] 15:40:31.021 [main] ERROR SearchStatistics - No statistics has been saved because EvoSuite failed to generate any test case [MASTER] 15:40:31.122 [main] ERROR TestGeneration - failed to write statistics data

Expected result

Should be able to resolve third party dependencies and generate test scenario.

Additional info

Please add any information of interest here below

gofraser commented 3 years ago

You need to tell EvoSuite where to find all the dependencies -- EvoSuite assumes that all dependences are already on the classpath. This means that either you need to provide a full classpath (including all jar dependencies) to the -projectCP option, or use $EVOSUITE -setup to tell it where to find the dependencies (or use the Maven plugin to automatically). According to your example command line, you are only passing in the classpath of the application classes, not its dependencies.