title: Test dynamodb using Mockito
body: <p>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</p>
  <pre><code>public Customer getCustomer(final String customerId) {
      DynamoDbTable&lt;Customer&gt; customerTable = getTable();
      Key key = Key.builder().partitionValue(customerId)
          .build();
      return customerTable.getItem(key);
    }
  
    private DynamoDbTable&lt;Customer&gt; getTable() {
      return dynamoDbEnhancedClient.table(&quot;Customer&quot;,
          TableSchema.fromBean(Customer.class));
    }
  </code></pre>
  <p>Following is the test i tried</p>
  <pre><code>
  @ExtendWith(MockitoExtension.class)
  class CustomerRepositoryTest {
  
    @Mock
    private DynamoDbEnhancedClient dynamoDbEnhancedClient;
    @Mock
    private DynamoDbClient dynamoDb;
    @Mock
    private DynamoDbTable&lt;Customer&gt; dyamoDbTable;
    @Mock
    private Key key;
  
    @InjectMocks
    private CustomerRepository repository;
  
    private final String TABLE_NAME = &quot;Customer&quot;;
  
    private final String customerId = &quot;123&quot;;
  
    @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(&quot;123&quot;);
      customer.setName(&quot;name&quot;);
      when(dynamoDbEnhancedClient.table(&quot;Customer&quot;,
          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));
    }
  
  }
  </code></pre>
  <p>i used <code>mock-maker-inline</code> extension as Key is final class and got the below error</p>
  <pre><code>
  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.
  </code></pre>
  <p>how do i test this</p>