Problem description- I am trying to test my login method which returns an object of AuthenticationResponse which contains four fields-username,jwt token,refresh token,and expiry Time for the jwt token.
public class **AuthenticationResponse** {
private String userName;
private String jwtToken;
private Instant expiresAt;
private String refreshToken;
}
My Login method(method to be tested) is in AuthService class.
@Service
public class AuthService
{
public AuthenticationResponse login(LoginRequest loginRequest)
{
//some logic going on...
Instant expiresAt=Instant.now().plusMillis(900*1000);
//some logic going on
return authenticationResponse;
}
}
My Test class goes like this->
public class AuthServiceTest
{
@InjectMocks
private AuthService authService;
@Test
public void loginTest_whenCredentialsAreCorrect()
{
///some logic goes over here...
AuthenticationResponse authenticationResponse=new AuthenticationResponse();
authenticationResponse.setJwtToken("jwttest");
authenticationResponse.setUserName("jack");
authenticationResponse.setRefreshToken("jwt1");
authenticationResponse.setExpiresAt(Instant.now().plusMillis(900000));
assertEquals(authenticationResponse,authService.login(loginRequest));
}
}
I have written my logic inside the method which assigns the value for expiresAt field like this
Instant expiresAt=Instant.now().plusMillis(900*1000)
Now,in how can I assign the same value for that field(expiresAt) while creating the object of AuthenticationResponse to assert it with the returned value from method.
I can't get any way to match the value of this field with the value returned from the method.
My Test fails showing this output:
org.opentest4j.AssertionFailedError:
Expected :AuthenticationResponse(userName=jack, jwtToken=jwttest, expiresAt=2020-07-30T13:40:45.828Z, refreshToken=jwt1)
Actual :AuthenticationResponse(userName=jack, jwtToken=jwttest, expiresAt=2020-07-30T13:40:45.840Z, refreshToken=jwt1)
Is there any way around to solve this problem??