assertionerror

o4hqfura  于 2021-06-30  发布在  Java
关注(0)|答案(1)|浏览(438)

我尝试测试在尝试登录新用户时是否抛出badcredationals和disableexception。控制器中的代码是:

@PostMapping("/login")
  public ResponseEntity<?> createAuthenticationToken(@RequestBody JwtRequest authRequest) {
    final String token;

    authenticate(authRequest.getUserName(), authRequest.getPassword());

    final MyUserDetails userDetails = userService.loadUserByUsername(authRequest.getUserName());
    token = jwtTokenUtil.generateToken(userDetails);

    return ResponseEntity.ok(new JwtResponse(token));
  }

  private void authenticate(String username, String pass) throws BadCredentialsException, DisabledException{
    try{

      authManager.authenticate(new UsernamePasswordAuthenticationToken(username,pass));
    }catch(DisabledException e){
      throw new DisabledException("User disabled");
    }catch(BadCredentialsException e){
      throw new BadCredentialsException("Wrong Username or password");
    }
  }

测试代码为:

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

    when(authManager.authenticate(new UsernamePasswordAuthenticationToken("someUsername", "somePassword")))
        .thenThrow(new BadCredentialsException("bad credentials"));

    controller.createAuthenticationToken(jwtRequest);

    verify(service, never()).loadUserByUsername("some");

  }

}

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

    when(authManager.authenticate(new UsernamePasswordAuthenticationToken("someUsername", "somePassword")))
        .thenThrow(new DisabledException("User_disabled"));

    controller.createAuthenticationToken(jwtRequest);

    verify(service, never()).loadUserByUsername("some");

当我尝试运行测试时,我总是得到下面的错误消息

java.lang.AssertionError: Expected exception: org.springframework.security.authentication.DisabledException

    at org.junit.internal.runners.statements.ExpectException.evaluate(ExpectException.java:32)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
    at org.mockito.internal.runners.JUnit45AndHigherRunnerImpl.run(JUnit45AndHigherRunnerImpl.java:37)
    at org.mockito.runners.MockitoJUnitRunner.run(MockitoJUnitRunner.java:62)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:160)
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
    at com.intellij.rt.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:33)
    at com.intellij.rt.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:230)
    at com.intellij.rt.junit.JUnitStarter.main(JUnitStarter.java:58)

Process finished with exit code 255

我现在知道如何解决这个问题了,有人能帮我解释一下问题是什么吗?谢谢

gdx19jrr

gdx19jrr1#

我认为这句话:

when(authManager.authenticate(new UsernamePasswordAuthenticationToken("someUsername", "somePassword")))

应改为:

when(authManager.authenticate(new UsernamePasswordAuthenticationToken("someUser", "somPass")))

相关问题