0

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;
    }
    }
aisy
  • 1
  • 1
  • Please [edit] your question to include the actual method(s) being tested too – knittl Aug 26 '23 at 07:40
  • Your asertion failure states that "9999…" was expected, but that value is nowhere to be found in your test – knittl Aug 26 '23 at 07:40
  • `.when(mockDynamoHeartsListDao).setHeartListPreferences(…)` doesn't look like a helper method (which I'd expect to be statically defined on a class), but like a regular method call on an object instance. – knittl Aug 26 '23 at 07:47
  • thanks! I just added the helper method. so how I should statically define the helper method within the unit test class? Why? I did mock the class (mockDynamoHeartsListDao) that contains the helper method. Isn't that enough? – aisy Aug 28 '23 at 13:48
  • That's a regular method, I'm not sure what would make it a "helper" method? You are not showing how `addUserToAllowList` is using the `DynamoHouseListDao` (Heart? House?). How are `service` and `mockSharedHeartsListDynamo` related? If I have to guess, the problem is similar to [Why are my mocked methods not called when executing a unit test?](https://stackoverflow.com/questions/74027324/why-are-my-mocked-methods-not-called-when-executing-a-unit-test). Please [edit] the question to show how you connect the service and the dao, thanks – knittl Aug 28 '23 at 13:58

0 Answers0