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.
1条答案
按热度按时间lf3rwulv1#
在代码中需要解决两个问题:模拟静态方法和模拟对象上的多个链接调用。请在下面找到带有内联注解的代码以及一些参考资料以供进一步阅读。重要提示:应使用
mockito-inline
(请参见下面的文档链接)。我已经创建了a commit in a GitHub repository,在这里你可以找到上面的代码,以及
mockito-inline
依赖引用和一些伪类,这些伪类代表了你的问题中缺少的代码(这并不重要,因为它都是模拟的)。