![](/img/trans.png)
[英]How to assertEquals on a value that is passed in as a String against a value which is a double or float
[英]assertEquals the returned value of method which returns a dynamic value
问题描述 - 我正在尝试测试我的登录方法,它返回一个 object 的AuthenticationResponse ,其中包含四个字段 - 用户名、jwt 令牌、刷新令牌和 jwt 令牌的到期时间。
public class **AuthenticationResponse** {
private String userName;
private String jwtToken;
private Instant expiresAt;
private String refreshToken;
}
我的登录方法(要测试的方法)在 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;
}
}
我的测试 class 是这样的->
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));
}
}
我已经在为 expiresAt 字段分配值的方法中编写了我的逻辑,如下所示
Instant expiresAt=Instant.now().plusMillis(900*1000)
现在,如何在创建 AuthenticationResponse 的 object 时为该字段(expiresAt)分配相同的值,以使用方法的返回值对其进行断言。
我无法找到任何方法来将此字段的值与从该方法返回的值相匹配。
我的测试失败,显示此 output:
org.opentest4j.AssertionFailedError:
预期:AuthenticationResponse(userName=jack, jwtToken=jwttest, expiresAt=2020-07-30T13:40:45.828Z, refreshToken=jwt1)
实际:AuthenticationResponse(userName=jack, jwtToken=jwttest, expiresAt=2020-07-30T13:40:45.840Z, refreshToken=jwt1)
有什么办法可以解决这个问题吗??
您可以模拟 expiresAt 值。 请参阅下面的链接以找到各种方法。 https://www.baeldung.com/java-override-system-time
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.