コード例 #1
0
  @Test
  public void partialMocking() {
    // use partial mock
    final LoginService partialLoginService = new LoginService();
    partialLoginService.setLoginDao(loginDao);
    loginController.loginService = partialLoginService;

    final UserForm userForm = new UserForm();
    userForm.username = "******";
    // let service's login use implementation so let's mock DAO call
    new Expectations() {
      {
        loginDao.login(userForm);
        result = 1;
        // no expectation for loginService.login
        partialLoginService.setCurrentUser("foo");
      }
    };

    String login = loginController.login(userForm);

    Assert.assertEquals("OK", login);
    // verify mocked call
    new FullVerifications(partialLoginService) {};
    new FullVerifications(loginDao) {};
  }
コード例 #2
0
  @Test
  public void argumentMatching() {
    final UserForm userForm = new UserForm();
    userForm.username = "******";
    // default matcher
    new Expectations() {
      {
        loginService.login((UserForm) any);
        result = true;
        // complex matcher
        loginService.setCurrentUser(
            withArgThat(
                new BaseMatcher<String>() {
                  @Override
                  public boolean matches(Object item) {
                    return item instanceof String && ((String) item).startsWith("foo");
                  }

                  @Override
                  public void describeTo(Description description) {
                    // NOOP
                  }
                }));
      }
    };

    String login = loginController.login(userForm);

    Assert.assertEquals("OK", login);
    new FullVerifications(loginService) {};
  }
コード例 #3
0
  @Test
  public void assertOnlyOneMethodHasBeenCalled() {
    final UserForm userForm = new UserForm();
    userForm.username = "******";
    new Expectations() {
      {
        loginService.login(userForm);
        result = false;
        // no expectation for setCurrentUser
      }
    };

    String login = loginController.login(userForm);

    Assert.assertEquals("KO", login);
    new FullVerifications(loginService) {};
  }
コード例 #4
0
  @Test
  public void assertTwoMethodsHaveBeenCalled() {
    final UserForm userForm = new UserForm();
    userForm.username = "******";
    new Expectations() {
      {
        loginService.login(userForm);
        result = true;
        loginService.setCurrentUser("foo");
      }
    };

    String login = loginController.login(userForm);

    Assert.assertEquals("OK", login);
    new FullVerifications(loginService) {};
  }