AAA { public String checkUser(UserId userId) { final Map<UserId, User> users = tenantManage.getUsers(); String errorInfo = null; final User user = (users != null) ? users.get(userId) : null; if (user == null) { errorInfo = "The user is not exist."; } return errorInfo; } AAA(TenantManage tenantManage); String checkUser(UserId userId);  }
@Test public void testCheckUser() throws Exception { UserId userId = mock(UserId.class); User user = mock(User.class); Map<UserId, User> users = new HashMap<UserId, User>(); when(tenantManage.getUsers()) .thenReturn(null) .thenReturn(users); Assert.assertTrue(aaa.checkUser(userId).equals("The user is not exist.")); verify(tenantManage).getUsers(); users.put(userId, user); Assert.assertTrue(users != null); Assert.assertTrue(aaa.checkUser(userId) == null); verify(tenantManage,times(2)).getUsers(); }