mardi 25 septembre 2018

Mockito shows wrong UnnecessaryStubbingException

I am using mockito 2.15 to mock some beans in my spring boot application. After I updated to the newest version, mockito hinted that I may use inappropriate stubbings which is fine for me so far.

But I think, something is cached or buggy here, because after commenting out EVERY stubbing, my IDE (IntelliJ IDEA 2018.2) still returns the UnnecessaryStubbingException at the exact same point like there was code.

Here's the test method:

@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());
    }

At the line that says when(twitter.userOperations()).thenReturn(userOperations); It shows the error.

org.mockito.exceptions.misusing.UnnecessaryStubbingException: Unnecessary stubbings detected in test class: TwitterImportServiceTest Clean & maintainable test code requires zero unnecessary code. Following stubbings are unnecessary (click to navigate to relevant line of code):

I tried restarting the IDE and invalidating its cache, closed and re-opened the project, did a gradle clean. I honestly have no idea, what I could do as well

Aucun commentaire:

Enregistrer un commentaire