我正在使用Mockito 2.15模拟我的Spring Boot应用程序中的某些bean。更新到最新版本后,mockito暗示我可能会使用不合适的存根,到目前为止对我来说还不错。
但是我认为这里有些缓存或错误,因为在注释掉所有存根之后,我的IDE(IntelliJ IDEA 2018.2)仍然在与代码完全相同的位置返回UnnecessaryStubbingException。
这是测试方法:
@Test
public void testLoadData() {
String name = "The name";
String profilePicture = "http://url.pic.com";
Tweet tweetOne = new Tweet(123L, "text", new Date(), name, "http://url.profile.com", 0L, 0L, "de", "twitter");
Tweet tweetTwo = new Tweet(456L, "text", new Date(), name, "http://url.profile.com", 0L, 0L, "de", "twitter");
Tweet tweetThree = new Tweet(789L, "text", new Date(), name, "http://url.profile.com", 0L, 0L, "de", "twitter");
List<Tweet> tweets = new PagedList<>(Arrays.asList(tweetOne, tweetTwo, tweetThree), null, null);
// when(connectionFactory.getOAuthOperations()).thenReturn(oAuth1Template);
// when(connectionFactory.getOAuthOperations().exchangeForAccessToken(any(), any())).thenReturn(token);
// when(twitter.userOperations()).thenReturn(userOperations);
// when(userOperations.getScreenName()).thenReturn(name);
// when(userOperations.getUserProfile()).thenReturn(userProfile);
// when(userOperations.getUserProfile().getProfileImageUrl()).thenReturn(profilePicture);
// when(twitter.timelineOperations()).thenReturn(timelineOperations);
// when(twitter.timelineOperations().getUserTimeline(anyInt(), anyLong(), anyLong()))
// .thenReturn(tweets)
// .thenReturn(emptyList());
TwitterImportEntity result = twitterImportService.loadData();
assertThat(result).isNotNull();
assertThat(result.getIdentifier()).isEqualTo(TWITTER.getType());
assertThat(result.getAccessToken()).isNotEmpty();
assertThat(result.getAccessToken()).isEqualTo(ACCESS_TOKEN);
assertThat(result.getAccessTokenSecret()).isNotEmpty();
assertThat(result.getAccessTokenSecret()).isEqualTo(ACCESS_TOKEN_SECRET);
assertThat(result.getName()).isNotEmpty();
assertThat(result.getName()).isEqualTo(name);
assertThat(result.getProfilePicture()).isNotEmpty();
assertThat(result.getProfilePicture()).isEqualTo(profilePicture);
assertThat(result.getTweets()).isNotEmpty();
assertThat(result.getTweets()).hasSize(tweets.size());
}
在when(twitter.userOperations()).thenReturn(userOperations);
行中
它显示了错误。
org.mockito.exceptions.misusing.UnnecessaryStubbingException: 在测试类中检测到不必要的存根:TwitterImportServiceTest 干净且可维护的测试代码需要零个不必要的代码。 不需要以下存根(单击以导航到相关代码行):
我尝试重新启动IDE并使其缓存无效,关闭并重新打开该项目,进行了清理。老实说,我不知道我还能做什么