mardi 2 mars 2021

junit nested mock injections for characterization tests

I need to be able to mock nested classes that have been injected using junit so i can create characterazation tests this is the class structure i have:

class 1 {
    @Inject private Class2  class2 ;
}

class 2 {
    @Inject private Class3  class3 ;
}

my test suite needs to run from the top down in order to fully test my system as it is an old system and i need to write tests starting from the top in order to gradually refactor it.

Test suite:

@RunWith(MockitoJUnitRunner.class)
public class Class1Test {
    @Mock
    Class3 class3;//this doesn't work object remains null

    @Mock
    Class2 class2; //this injection works and the object isn't null inside of class1

    @InjectMocks
    public Class1 class1;

    @Before
    public void init() {
        MockitoAnnotations.initMocks(this);
    }
    @Test 
public void testFunction()
{
assertNotNull(class1.exampleFunction());
}

}

I know the correct way is to stub dependencies, but this code base is far to large to have to write unit tests for every layer, i need to be able to write tests in junit so that way i can run these tests while refactoring the code base to make sure i haven't broken anything, hence why i haven't used postman since i need it to be quick.

on java 8 @inject is a javax annotation using junit 4 but i think i can use junit5 if i need to. the project is a j2ee (java ee ) application

Aucun commentaire:

Enregistrer un commentaire