assertEquals方法的作用是比较返回的动态值的值。

huangapple go评论82阅读模式
英文:

assertEquals the returned value of method which returns a dynamic value

问题

问题描述-
我试图测试我的登录方法,该方法返回一个AuthenticationResponse对象,其中包含四个字段-用户名、JWT令牌、刷新令牌和JWT令牌的到期时间。

public class **AuthenticationResponse** {
    private String userName;
    private String jwtToken;
    private Instant expiresAt;
    private String refreshToken;
}

我的登录方法(要测试的方法)位于AuthService类中。

@Service
public class AuthService
{
    public AuthenticationResponse login(LoginRequest loginRequest)
    {
       //一些逻辑...
       Instant expiresAt=Instant.now().plusMillis(900*1000);
        //一些逻辑...
        return authenticationResponse;

    }
}

我的测试类如下:

public class AuthServiceTest
{
    @InjectMocks
    private AuthService authService;
    @Test
    public void loginTest_whenCredentialsAreCorrect()
    {
      //一些逻辑...
      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对象时分配相同的值给该字段(expiresAt),以便将其与方法返回的值进行断言。

我无法找到任何方法来匹配该字段的值与方法返回的值。

我的测试失败,显示以下输出:

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)
<点击查看差异>

是否有解决此问题的方法?

英文:

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(&quot;jwttest&quot;);
      authenticationResponse.setUserName(&quot;jack&quot;);
      authenticationResponse.setRefreshToken(&quot;jwt1&quot;);
    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)
<Click to see difference>

Is there any way around to solve this problem??

答案1

得分: 0

你可以模拟 expiresAt 的值。
请参考下面的链接找到不同的方法来做这件事。
https://www.baeldung.com/java-override-system-time

英文:

You can mock the expiresAt value.
Please refer below link to find various methods of doing it.
https://www.baeldung.com/java-override-system-time

huangapple
  • 本文由 发表于 2020年7月30日 20:47:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/63173506.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定