Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8396363
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T20:31:06+00:00 2026-06-09T20:31:06+00:00

In Mockito, is there a way to verify that there are no more interactions

  • 0

In Mockito, is there a way to verify that there are no more interactions on any mock I have created?

For example:

public void test()
{
  ...
  TestObject obj = mock(TestObject);
  myClass.test();
  verifyNoMoreInteractionsWithMocks();  <-------
}

Is there such a method?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T20:31:07+00:00Added an answer on June 9, 2026 at 8:31 pm

    Since verifyNoMoreInteractions take an array of object we have to find a way to get all the created mocks.

    You can create this class

    public class MocksCollector {
        private final List<Object> createdMocks;
    
        public MocksCollector() {
            createdMocks = new LinkedList<Object>();
            final MockingProgress progress = new ThreadSafeMockingProgress();
            progress.setListener(new CollectCreatedMocks(createdMocks));
        }
    
        public Object[] getMocks() {
            return createdMocks.toArray();
        }
    }
    

    and then use it in your test :

    public class ATest {
        private final MocksCollector mocksCollector = new MocksCollector();
        
        @Test
        public void test() throws Exception {
            A a1 = mock(A.class);
            A a2 = mock(A.class);
            A a3 = mock(A.class);
            assertEquals("wrong amount of mocks", 3, mocksCollector.getMocks().length);
            verifyNoMoreInteractions(mocksCollector.getMocks());
            a3.doSomething();
            verifyNoMoreInteractions(mocksCollector.getMocks()); // fail
        }
    }
    

    or with annotations :

    @RunWith(MockitoJUnitRunner.class)
    public class A2Test {
        private final MocksCollector mocksCollector = new MocksCollector();
    
        @Mock
        private A a1;
        @Mock
        private A a2;
        @Mock
        private A a3;
    
        @Test
        public void test() throws Exception {
            assertEquals("wrong amount of mocks", 3, mocksCollector.getMocks().length);
            verifyNoMoreInteractions(mocksCollector.getMocks());
            a2.doSomething();
            verifyNoMoreInteractions(mocksCollector.getMocks()); // fail
        }
    }
    

    It works but it adds a dependency on mockito internal.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For example, I have the following code: SomeClass stub = Mockito.mock(SomeClass.class); After that, stub
Using Mockito or another similar framework. Is there a way to mock a package-private
I have simple test case: @Test public void test() throws Exception{ TableElement table =
I'm currently studying the Mockito framework and I've created several test cases using Mockito.
I would like to do a test like: http://stevelibonati.wordpress.com/2012/02/05/spring-mvc-test-with-a-sprinkle-of-mockito/ My problem is that I
I have a stub that is used across different test cases and I need
Hello I would like to know, if there is any way to dynamicaly modify
Is there any good feature in EasyMock that is not there or not properly
Possible Duplicate: Intercept object on method invocation with Mockito I have an class that
Is there a way to declare a test dependency in the dependencies.yml file for

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.