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 8121219
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T05:25:08+00:00 2026-06-06T05:25:08+00:00

If I create a mock in my spring context file using Springockito as described

  • 0

If I create a mock in my spring context file using Springockito as described here, how do I mock some behavior for it?

What I’m trying to do:

  1. ClassA is being tested.
  2. ClassB is autowired in ClassA.
  3. ClassB is being mocked with Springockito.
  4. ClassA needs ClassB to do something in its PostConstruct.
  5. I need to mock ClassB to do that something, since it can’t and shouldn’t really do it.

Doing this is straight forward without using Springockito (using Mockito straight up), but I need to autowire these beans and use Spring in my tests. Any help is appreciated.

  • 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-06T05:25:10+00:00Added an answer on June 6, 2026 at 5:25 am

    I’m not familiar with Springockito, but it looks interesting for some narrow cases – namely integration testing with mocking just a bit.

    Anyway, it looks like for a straightforward use case you extend AbstractJUnit4SpringContextTests, you could also autowire ClassB in your test just like you do in ClassA. Then you could define your expected behavior for ClassB in your setup method.

    But I think that you need to set up some behavior for the ClassB bean before you get access to it in your setup method. In that case, you may need another bean to set up ClassB to do the expected behavior. So your testContext.xml would have something like this in it:

    <bean id="classA" class="com.jarvis.ClassA" depends-on="classBMockSetter" />
    <mockito:mock id="classB" class="com.jarvis.ClassB" />
    <bean id="classBMockSetter" class="com.jarvis.test.ClassBMockSetter">
      <property name="classB" ref="classB" />
    </bean>
    

    The ClassBMockSetter would look something like:

    public class ClassBMockSetter {
      private ClassB classB;
      public void setClassB(ClassB classB) {
        this.classB = classB;
        given(classB.foo()).willReturn(true);
        given(classB.bar()).willReturn(42);
      }
    }
    

    I think that would work, but at that point, isn’t it easier to just hand-code your mock ClassB?

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

Sidebar

Related Questions

I'm using EasyMock to create mock that is one of private parameters (without setter)
I'm trying to create a mock HttpContextBase for unit test. var fakePrinciple = new
Here's my problem - I am using MvcMailer to create nicely formatted emails using
I'm trying to create mock of TransactionTemplate var tTemplate = MockRepository.GenerateMock<TransactionTemplate>(); var tDelegate =
I am using EasyMock to create mock classes in test cases. expect(entity.getType()).andReturn(string); Type belongs
The goal is to create a mock class which behaves like a db resultset.
if I do not use a mock framework to create a mock, but write
The new Fakes framework in VS11 allows you to create fake (mock or stub)
I am writing unit tests for service layer in my spring application. Here is
I am trying to test the logic from some existing classes. It is not

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.