コード例 #1
0
  @Test
  public void testSaveMongoUser() {
    String[] ids = new String[1];
    when(mongoUserDao.save(
            argThat(
                new ArgumentMatcher<MongoUser>() {
                  @Override
                  public boolean matches(Object argument) {
                    return argument instanceof MongoUser;
                  }
                })))
        .thenAnswer(
            new Answer<MongoUser>() {
              @Override
              public MongoUser answer(InvocationOnMock invocation) throws Throwable {
                MongoUser user = invocation.getArgumentAt(0, MongoUser.class);
                user.setId(ids[0] = UUID.randomUUID().toString());
                return user;
              }
            });
    MongoUser mongoUser = new MongoUser();
    mongoUser.setName("test");
    MongoUser saved = service.saveMongoUser(mongoUser).toBlocking().first();

    Assert.assertNotNull(saved);
    Assert.assertEquals(mongoUser.getName(), saved.getName());
    Assert.assertEquals(ids[0], saved.getId());
  }
コード例 #2
0
  @Test
  public void testGetMongoUser() {
    List<MongoUser> list = new ArrayList<MongoUser>();
    MongoUser user = new MongoUser();
    user.setId(UUID.randomUUID().toString());
    user.setName("user");
    list.add(user);

    when(mongoUserDao.findByName(anyString())).thenReturn(list);
    when(mongoUserDao.findByName(null))
        .thenThrow(new NullPointerException("username cannot be null"));

    List<MongoUser> listRs = service.findMongoUserByName("user").toBlocking().first();

    verify(mongoUserDao).findByName(anyString());

    Assert.assertNotNull(listRs);
    Assert.assertTrue(listRs.containsAll(list));

    expectedException.expect(NullPointerException.class);
    expectedException.expectMessage("username cannot be null");
    service.findMongoUserByName(null).toBlocking().first();
  }