dimanche 25 novembre 2018

Spring Test :Unit Test case for successful authentication

I have authentication provider(Java class) which has authentication function which is responsible to get the authenticatedToken.

Below is the function :

 @Override

    public Authentication authenticate(Authentication authentication) throws AuthenticationException {

        try {

            AuthenticationResponse AuthenticationResponse = this.AuthenticationService.authenticate((String) authentication.getPrincipal(), (String) authentication.getCredentials());

            UsernamePasswordAuthenticationToken authenticatedToken = new UsernamePasswordAuthenticationToken(authentication.getPrincipal(), null, createGrantedAuthorities(Role.ADMIN, Role.USER));

            authenticatedToken.setDetails(AuthenticationResponse);

            return authenticatedToken;

        } catch (LoginAuthenticationException ex) {

            // todo handle exceptions

            LOGGER.error(Error.UNABLE_TO_AUTHENTICATE_USER.getErrorLogEvent(), ex);

            throw ex;

        } catch (Exception ex) {

            LOGGER.error(Error.UNABLE_TO_AUTHENTICATE_USER.getErrorLogEvent(), ex);

            throw ex;

        }

    }

I'm trying to write the Unit Test for the same. below are the test file I started with .. but stuck at the point.

import static org.junit.Assert.*;

@RunWith(MockitoJUnitRunner.class)

public class AuthenticationProviderTest {

@InjectMocks

AuthenticationProvider authenticationProvider;



@Mock

private AuthenticationService authenticationService;



@Test

public void testAuthenticate_success() throws Exception {
       AuthenticationProvider test = new authenticationProvider();
       assertEquals("True", authenticationProvider.authenticate(), ""Mix of number and A-Z/a-z);

    assertNotNull(authenticationProvider.authenticate);

     ??????????????????????????????????????????????????????????

}

}

As a new bee to Java I might be absolutely wrong about writing the unit test .. any suggestion and help would be appreciated.

Aucun commentaire:

Enregistrer un commentaire