Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Test dynamodb using Mockito

I'm using dynamodb sdk2 with java. I have model, controller and repository and need to unit test them, following is the repository method i'm testing

public Customer getCustomer(final String customerId) {
    DynamoDbTable<Customer> customerTable = getTable();
    Key key = Key.builder().partitionValue(customerId)
        .build();
    return customerTable.getItem(key);
  }

  private DynamoDbTable<Customer> getTable() {
    return dynamoDbEnhancedClient.table("Customer",
        TableSchema.fromBean(Customer.class));
  }

Following is the test i tried


@ExtendWith(MockitoExtension.class)
class CustomerRepositoryTest {

  @Mock
  private DynamoDbEnhancedClient dynamoDbEnhancedClient;
  @Mock
  private DynamoDbClient dynamoDb;
  @Mock
  private DynamoDbTable<Customer> dyamoDbTable;
  @Mock
  private Key key;

  @InjectMocks
  private CustomerRepository repository;

  private final String TABLE_NAME = "Customer";

  private final String customerId = "123";

  @BeforeEach
  public void setup() {
    dynamoDbEnhancedClient = DynamoDbEnhancedClient.builder().dynamoDbClient(dynamoDb).build();
    dyamoDbTable = dynamoDbEnhancedClient.table(TABLE_NAME, TableSchema.fromBean(Customer.class));
    key = Key.builder().partitionValue(customerId).build();
  }

  @Test
  void testGetDoorState() {
    Customer custmer = new Customer();
    customer.setCustomerId("123");
    customer.setName("name");
    when(dynamoDbEnhancedClient.table("Customer",
        TableSchema.fromBean(Customer.class))).thenReturn(dyamoDbTable);
    when(Key.builder().partitionValue(customerId).build()).thenReturn(key);
    when(dyamoDbTable.getItem(key)).thenReturn(customer);
    assertEquals(customer, repository.getCustomer(customerId));
  }

}

i used mock-maker-inline extension as Key is final class and got the below error


org.mockito.exceptions.misusing.MissingMethodInvocationException: 
when() requires an argument which has to be 'a method call on a mock'.
For example:
    when(mock.getArticles()).thenReturn(articles);

Also, this error might show up because:
1. you stub either of: final/private/equals()/hashCode() methods.
   Those methods *cannot* be stubbed/verified.
   Mocking methods declared on non-public parent classes is not supported.
2. inside when() you don't call method on mock but on some other object.

how do i test this


1 Answers

I think you might be missing that the @Mock variables don't need to be defined. In spring these Mocks are there to allow you to expect certain responses from calls to methods within these Classes. For example this line:

dynamoDbEnhancedClient = DynamoDbEnhancedClient.builder().dynamoDbClient(dynamoDb).build();

uses dynamoDb, a @Mock variable as a parameter to a call in your builder. This is incorrect. In fact, to my understanding, you have no need to initialize your @Mock Objects. Remove those from the setup, and let's see what you get.

like image 95
CrazedCoder Avatar answered Sep 03 '26 23:09

CrazedCoder



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!