You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Java登录接口测试指定异常时出现AssertionError问题求助

Fixing Your AssertionError in Authentication Tests

Let's break down why your tests are failing and how to fix them:

Key Issues Identified

  1. Argument Mismatch in Mock Setup
    Your mock for authManager.authenticate() uses hardcoded values ("someUsername", "somePassword"), but your test passes a JwtRequest with "someUser" and "somPass". Mockito only triggers the mocked behavior when the arguments exactly match what's expected—so your exception never gets thrown, leading to the AssertionError.

  2. Incorrect Verify Argument
    Your verify statement checks for "some" as the username, but your test uses "someUser"—this would fail even if the exception was thrown correctly.

  3. Minor Inconsistency in Test Declaration
    Your second test doesn't declare throws Exception (though this isn't strictly required for runtime exceptions, it's good to align with your first test for consistency).

Corrected Test Code

@Test(expected = BadCredentialsException.class)
public void shouldThrowBadCredentialsExceptionWhenTryToAuthWithBadCredential() throws Exception {
    String testUsername = "someUser";
    String testPassword = "somPass";
    JwtRequest jwtRequest = new JwtRequest(testUsername, testPassword);

    // Match the exact arguments passed to authManager
    when(authManager.authenticate(new UsernamePasswordAuthenticationToken(testUsername, testPassword)))
        .thenThrow(new BadCredentialsException("bad credentials"));

    controller.createAuthenticationToken(jwtRequest);

    // Verify against the correct username
    verify(service, never()).loadUserByUsername(testUsername);
}

@Test(expected = DisabledException.class)
public void shouldThrowDisabledExceptionWhenTryToAuthWithDisabledUser() throws Exception {
    String testUsername = "someUser";
    String testPassword = "somPass";
    JwtRequest jwtRequest = new JwtRequest(testUsername, testPassword);

    // Match the exact arguments passed to authManager
    when(authManager.authenticate(new UsernamePasswordAuthenticationToken(testUsername, testPassword)))
        .thenThrow(new DisabledException("User_disabled"));

    controller.createAuthenticationToken(jwtRequest);

    // Verify against the correct username
    verify(service, never()).loadUserByUsername(testUsername);
}

Additional Notes

  • If you don't need to test exact credentials (just that any bad credentials trigger the exception), you can use Mockito matchers like anyString() instead of hardcoding values:
    when(authManager.authenticate(new UsernamePasswordAuthenticationToken(anyString(), anyString())))
        .thenThrow(new BadCredentialsException("bad credentials"));
    
  • Ensure your authManager is properly mocked (using @Mock or Mockito.mock(AuthenticationManager.class)) in your test class—otherwise you'll get a NullPointerException instead of the intended exception.

内容的提问来源于stack exchange,提问作者snzew

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.11 07:59:54