Search code examples
javajmockit

Java - JMockit testing 2 injected classes with the same field names


I created a test class for abstract ClassA. But i encountered an error Missing @Injectable for field "ClassC" in ClassB.

I tried renaming one of the ClassC field names and it worked. But how can I fix this error without renaming field name of ClassC?

My codes below :

abstract ClassA extends ClassB {

    @Inject
    ClassC classC;
}

abstract ClassB {

    @Inject
    ClassC classC; 
}

//---------------- Test class for ClassA* 

@RunWith(JMockit.class) 

ClassATest {

   @Injectable
   ClassC classC;
}

Solution

  • You can use Tested annotion and set fullyInitialized = true.

    public final class ExampleIntegrationTest {
        @Tested(fullyInitialized = true) 
        private ClassA classA;
    
        @Mocked
        private ClassC classC;
    
        //testMethod
    }