My stack uses java, mocikito, junit, and testng. I'm fairly new to unit testing in general so it would appreciated if someone could explain me what's wrong with this and why in a helpful way.
I get a [testng] java.lang.AssertionError: expected [999999999999] but found [0]
in this line assertEquals(actualListsPreferences.getUserId().longValue(), USER_ID);
The problem is that .setHomePreferences(
is a helper method outside of the class that is being tested, namely DynamoOtherDao
. The code runs fine when that helper method is within the class that is being test (this.service
) and there is no need to mock it doReturn(mockHome)...
I've spent too many hours trying to figure this out, any clues would help. Thanks.
ArgumentCaptor<Home> argumentPojoHome = ArgumentCaptor.forClass(Home.class);
ArgumentCaptor<Apt> argumentPojoApt = ArgumentCaptor.forClass(Apt.class);
ArgumentCaptor<DynamoDBTransactionWriteExpression> argumentWriteExp = ArgumentCaptor.forClass(DynamoDBTransactionWriteExpression.class);
doReturn(mockHome)
.when(mockDynamoOtherDao)
.setHomePreferences(USER_ID, LIGHTS, KITCHEN, LIVING_ROOM);
this.service.addUserToAllowList(USER_ID, ROLE, CAN_SHARE, USER_ID, LIGHTS, KITCHEN, LIVING_ROOM);
verify(this.mockTransactionWriteRequest, times(2)).addPut(argumentPojoHome.capture(), Matchers.isA(DynamoDBTransactionWriteExpression.class));
verify(this.mockTransactionWriteRequest, times(2)).addPut(argumentPojoApt.capture(), Matchers.isA(DynamoDBTransactionWriteExpression.class));
Home actualHomePreferences = argumentPojoHome.getAllValues().get(0);
Apt actualAptPreferences = argumentPojoApt.getAllValues().get(1);
assertEquals(argumentPojoHome.getUserId().longValue(), USER_ID);
assertEquals(argumentPojoHome.getLights(), LIGHTS);
assertEquals(argumentPojoHome.getKitchen(), Kitchen.PUBLIC);
assertEquals(argumentPojoHome.getLivingRoom(), LIVING_ROOM);
// Check the captured TransactionWriteRequest and its content
verify(this.mockDynamoDBMapper).transactionWrite(mockTransactionWriteRequest);
I have tried mocking the helper method:
doReturn(mockSharedHeartsListDynamo)
.when(mockDynamoHeartsListDao)
.setHeartListPreferences(USER_ID, INFLUENCER_ID, PUBLIC_STATUS, PEN_NAME);
but that doesn't help and I'm pretty sure its because is when assertEquals
is taking argumentPojoHome
which should capture arguments when this.service.addUserToAllowList(
is called, but it isn't because of the helper method.
edit:
this is the helper method from another class:
public class DynamoHouseListDao {
public HouseListDynamo setHouseListPreferences(long userAccountId, String lights, String Kitchen, String LivingRoom) {
ZonedDateTime now = ZonedDateTime.now(DateTypeConverter.zoneOffset);
HouseListDynamo houseListDynamo = new HouseListDynamo.Builder(userAccountId,null).build();
heartsListDynamo.setKitchen9("");
heartsListDynamo.set...
return heartsListDynamo;
}
}