コード例 #1
0
 Item makeMockItemWithIDAndName(String id, String name) {
   Item item = mock(Item.class);
   when(item.id()).thenReturn(id);
   when(item.name()).thenReturn(name);
   when(item.midSentenceCasedName()).thenReturn(name);
   when(item.useWith(org.mockito.Mockito.any(Item.class))).thenReturn("");
   return item;
 }
コード例 #2
0
  @Test
  public void getUsersFromExternalService() {
    ResponseEntity<List<User>> responseEntity =
        new ResponseEntity<>(Arrays.asList(User.builder().name("nisse").build()), HttpStatus.OK);
    org.mockito.Mockito.when(
            restTemplateMock.exchange(
                eq("http://localhost:10280/users"),
                eq(HttpMethod.GET),
                eq(null),
                eq(new ParameterizedTypeReference<List<User>>() {})))
        .thenReturn(responseEntity);

    List<User> userList = userManagerService.getUsers();
    verify(restTemplateMock, times(1))
        .exchange(
            eq("http://localhost:10280/users"),
            eq(HttpMethod.GET),
            eq(null),
            eq(new ParameterizedTypeReference<List<User>>() {}));
    assertNotNull(userList);
    assertEquals("nisse", userList.get(0).getName());
  }