mardi 21 avril 2020

Mockito incorrectly injecting string value

I have created an object in my test class, testObj, and mockDao is a dao object that I have mocked.

Function to test is something like:

MyDao myDao;

String myFunc(String param1, String param2) {
    MyObj realObj = new MyObj("param1", "param2");
    return myDao.someFunction(realObj);
}

UnitTest class something like:

@Mock
MyDao mockDao;
@InjectMock
MyComponent component;

MyObj testObj = new MyObj("param1", "param2");

@Test
public void test() {
    Mockito.when(mockDao.someFunction(testObj)).thenReturn(“123”);
    String returnedValue = component.myFunc("param1","param2");
    Assert.assertEquals(returnedValue, "123");
}

this should not inject “123” in my actual class, since the object that is created in actual class (realObj) is different from the testObj.

However, this is injecting "123" and my test is passing. While ideally it should fail.

How is it possible that this injection is happening?

Aucun commentaire:

Enregistrer un commentaire