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

  • SEARCH
  • Home
  • 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 8059437
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T09:40:20+00:00 2026-06-05T09:40:20+00:00

I started to learn about Mockito only today. I wrote some simple test (with

  • 0

I started to learn about Mockito only today. I wrote some simple test (with JUnit, see below), but I can’t figure out how can I use mock object inside Spring’s managed beans. What is best practices for working with Spring. How should I inject mocked dependency to my bean?

You can skip this till back to my question.

First of all, what I’ve learned.
This is very good article Mocks Aren’t Stubs that explains the basics (Mock’s checks behavior verification not state verification). Then there is a good example here Mockito
and here Easier mocking with mockito. We have explanation that Mockito’s mock objects are both mock and stub.

Here Mockito and here Matchers, you can find more examples.

This test

@Test
public void testReal(){
    List<String> mockedList = mock(List.class);
     //stubbing
     //when(mockedList.get(0)).thenReturn("first");

    mockedList.get(anyInt());
    OngoingStubbing<String> stub= when(null);
    stub.thenReturn("first");

    //String res = mockedList.get(0);
                //System.out.println(res);

     //you can also verify using argument matcher
     //verify(mockedList).get(anyInt());

    verify(mockedList);
    mockedList.get(anyInt());
}

works just fine.

Back to my question. Here Injecting Mockito mocks into a Spring bean somebody tries to use Springs ReflectionTestUtils.setField(), but then here Spring Integration Tests, Creating Mock Objects we have recommendation to change Spring’s context.

I didn’t really understand last two links… Can somebody explain to me what problem does Spring have with Mockito? What’s wrong with this solution?

@InjectMocks
private MyTestObject testObject

@Mock
private MyDependentObject mockedObject

@Before
public void setup() {
        MockitoAnnotations.initMocks(this);
}

https://stackoverflow.com/a/8742745/1137529

EDIT: I wasn’t really clear. I will provide 3 examples of code to clarify my self:
Suppose, we have bean HelloWorld with method printHello() and bean HelloFacade with method sayHello that forward calls to HelloWorld’s method printHello().

First example is using Spring’s context and without custom runner, using ReflectionTestUtils for dependency injection (DI):

public class Hello1Test  {
private ApplicationContext ctx;

@Before
public void setUp() {
    MockitoAnnotations.initMocks(this);
    this.ctx = new ClassPathXmlApplicationContext("META-INF/spring/ServicesImplContext.xml");
}



@Test
public void testHelloFacade() {
    HelloFacade obj = (HelloFacade) ctx.getBean(HelloFacadeImpl.class);
    HelloWorld mock = mock(HelloWorld.class);
    doNothing().when(mock).printHello();

    ReflectionTestUtils.setField(obj, "hello", mock);
    obj.sayHello();

    verify(mock, times(1)).printHello();
}

}

As @Noam pointed out thereis way to run it wihtout explicit call to MockitoAnnotations.initMocks(this);. I will also drop using of the Spring’s context on this example.

@RunWith(MockitoJUnitRunner.class)
public class Hello1aTest {


@InjectMocks
private HelloFacade obj =  new HelloFacadeImpl();

@Mock
private HelloWorld mock;


@Test
public void testHelloFacade() {
    doNothing().when(mock).printHello();
    obj.sayHello();
}

}

Another way to do this

public class Hello1aTest {

@Before
public void setUp() {
    MockitoAnnotations.initMocks(this);
}


@InjectMocks
private HelloFacadeImpl obj;

@Mock
private HelloWorld mock;


@Test
public void testHelloFacade() {
    doNothing().when(mock).printHello();
    obj.sayHello();
}

}

Noth, that in preivious example we have to manually instaniate HelloFacadeImpl and assign it to HelloFacade, beacuse HelloFacade is interface. In the last example, we can just declare HelloFacadeImpl and Mokito will instantiate it for us. The drawback of this approach that now, unit-under-test is impl-class and not interface.

  • 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-05T09:40:21+00:00Added an answer on June 5, 2026 at 9:40 am

    Honestly I am not sure if I really understand your question 😛 I will try to clarify as much as I can, from what I get from your original question:

    First, in most case, you should NOT have any concern on Spring. You rarely need to have spring involved in writing your unit test. In normal case, you only need to instantiate the system under test (SUT, the target to be tested) in your unit test, and inject dependencies of SUT in the test too. The dependencies are usually a mock/stub.

    Your original suggested way, and example 2, 3 is precisely doing what I am describing above.

    In some rare case (like, integration tests, or some special unit tests), you need to create a Spring app context, and get your SUT from the app context. In such case, I believe you can:

    1) Create your SUT in spring app ctx, get reference to it, and inject mocks to it

    @RunWith(SpringJUnit4ClassRunner.class)
    @ContextConfiguration("test-app-ctx.xml")
    public class FooTest {
    
        @Autowired
        @InjectMocks
        TestTarget sut;
    
        @Mock
        Foo mockFoo;
    
        @Before
        /* Initialized mocks */
        public void setup() {
            MockitoAnnotations.initMocks(this);
        }
    
        @Test
        public void someTest() {
             // ....
        }
    }
    

    or

    2) follow the way described in your link Spring Integration Tests, Creating Mock Objects. This approach is to create mocks in Spring’s app context, and you can get the mock object from the app ctx to do your stubbing/verification:

    @RunWith(SpringJUnit4ClassRunner.class)
    @ContextConfiguration("test-app-ctx.xml")
    public class FooTest {
    
        @Autowired
        TestTarget sut;
    
        @Autowired
        Foo mockFoo;
    
        @Test
        public void someTest() {
             // ....
        }
    }
    

    Both ways should work. The main difference is the former case will have the dependencies injected after going through spring’s lifecycle etc. (e.g. bean initialization), while the latter case is injected beforehands. For example, if your SUT implements spring’s InitializingBean, and the initialization routine involves the dependencies, you will see the difference between these two approach. I believe there is no right or wrong for these 2 approaches, as long as you know what you are doing.

    Just a supplement, @Mock, @Inject, MocktoJunitRunner etc are all unnecessary in using Mockito. They are just utilities to save you typing the Mockito.mock(Foo.class) and bunch of setter invocations.

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

Sidebar

Related Questions

When I started to learn about WPF and MVVM recently, I came across some
I just started to learn about mod rewrite process. Now, I got some problems
I started to read and learn about sockets, but I'm looking for a small
I've just started to learn about Asp.Net 3.5 compilation model, but most articles mostly
I have started to learn about python and is currently reading through a script
Well, I got problem here with opengl ES stuff (just started to learn about
Ok guys, I started to learn more about Jquery and now I could make
I started to really sit down and learn PHP about a week ago, so
I have just started to learn about the pros of foreign keys in database
I've started to learn Ruby recently, and was wondering about which version to learn.

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.