mockito 用于Springboot中JWT的generateToken()方法的Junit

0pizxfdo  于 2022-11-08  发布在  Spring
关注(0)|答案(1)|浏览(209)
public String generateToken(final String id) {

    Claims claims = Jwts.claims().setSubject(id);
    long nowMillis = System.currentTimeMillis();
    long expMillis = nowMillis + tokenValidity;
    Date exp = new Date(expMillis);

    return Jwts.builder().setClaims(claims).setIssuedAt(new Date(nowMillis)).setExpiration(exp)
            .signWith(SignatureAlgorithm.HS512, jwtSecret).compact();
}

现在我想为这个方法编写JUnit,我尝试如下,但是我得到了错误

@Test
    @Order(1)
    public void test_generateToken() throws JwtTokenMalformedException, JwtTokenMissingException {
        final String subject_id = "123456789";
        final Long tokenValidity = 180000L;
        final String jwtSecret = "jwtSecret";

        when(Jwts.claims().setSubject(subject_id)).thenReturn(new DefaultClaims()); //**line no: 10

        when(Jwts.builder().setClaims(claims).setIssuedAt(new Date(nowMillis)).setExpiration(exp)
                .signWith(SignatureAlgorithm.HS512, jwtSecret).compact()).thenReturn(new String());
    }

在第10行获取错误:

org.mockito.exceptions.misusing.MissingMethodInvocationException: 
when() requires an argument which has to be 'a method call on a mock'.
For example:
    when(mock.getArticles()).thenReturn(articles);

Also, this error might show up because:
1. you stub either of: final/private/equals()/hashCode() methods.
   Those methods *cannot* be stubbed/verified.
   Mocking methods declared on non-public parent classes is not supported.
2. inside when() you don't call method on mock but on some other >     object.
lf3rwulv

lf3rwulv1#

在代码中需要解决两个问题:模拟静态方法和模拟对象上的多个链接调用。请在下面找到带有内联注解的代码以及一些参考资料以供进一步阅读。重要提示:应使用mockito-inline(请参见下面的文档链接)。

void test_generateToken() {
    var tested = new TestedClass();
    var jwtSecret = "jwtSecret";
    var claims = new DefaultClaims();
    var mockedValue = "mocked value";
    var builder = mock(Jwts.JwtsBuilder.class, RETURNS_DEEP_STUBS);

    try (var mockedStatic = mockStatic(Jwts.class)) {
        // to mock static methods the object returned from the mockStatic method should be used
        mockedStatic.when(Jwts::claims)
                    .thenReturn(claims);
        mockedStatic.when(Jwts::builder)
                    .thenReturn(builder);
        // here we're not mocking static methods, so a standard when method is used
        // thanks to RETURNS_DEEP_STUBS we can mock a chain of method calls
        when(builder.setClaims(claims)
                    // other argument matchers like argThat() can be used here
                    .setIssuedAt(any())
                    .setExpiration(any())
                    .signWith(SignatureAlgorithm.HS512, jwtSecret)
                    .compact())
                .thenReturn(mockedValue);

        var result = tested.generateToken("value");

        assertEquals(mockedValue, result);
    }
}

我已经创建了a commit in a GitHub repository,在这里你可以找到上面的代码,以及mockito-inline依赖引用和一些伪类,这些伪类代表了你的问题中缺少的代码(这并不重要,因为它都是模拟的)。

相关问题