EvoSuite / evosuite

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

[need help] cannot mock private static final field when generate test #468

Closed jaksonlin closed 6 months ago

jaksonlin commented 6 months ago

Context

when i tried generate test in our code base, the mocking dependencies fails on the private static final fields like logger assets...

reporting Cannot use Mockito on xxxxx due to failed class initialization:

maybe we need mockito-inline support? but how to configure it correctly?

Steps to Reproduce

  1. create a class that has private static final fields external dependencies
  2. run test generation

EvoSuite Arguments

this is the gradle command setting

def command = [ 'evosuite.jar', '-class', className, '-Dno_runtime_dependency=true', "-Dsearch_budget=20", "-Dtest_dir=${generatedTestDir}", "-junit=5", "-Dhandle_static_fields=false", "-DCP_file_path=${classpathFile.absolutePath}" ]

Current Result

ERROR FunctionalMockStatement - Cannot use Mockito on class petstore.repository.TXContext due to failed class initialization: Could not initialize class petstore.logging.LogFactory

all generation tests only contains one assertion on: no meaningful assertion but class not found

public void test0()  throws Throwable  {
      PetStoreService petStoreService0 = new PetStoreService ();
      Instant instant0 = MockInstant.now();
      Date date0 = Date.from(instant0);
      // Undeclared exception!
      try { 
        onlineUserService0.getPetStoreList(date0, date0);
        fail("Expecting exception: NoClassDefFoundError");

      } catch(NoClassDefFoundError e) {
         //
         // Could not initialize class testdata.PetStoreDataService
         //
      }
  }

Expected result

mock create successful, and test generation ok

jaksonlin commented 6 months ago

resolve after reading the classloader code.