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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T21:19:26+00:00 2026-06-12T21:19:26+00:00

What is method equivalent for the following: @Mock MyType1 myType1; @Autowired @InjectMocks MyType2 myType2;

  • 0

What is method equivalent for the following:

@Mock
MyType1 myType1;

@Autowired
@InjectMocks
MyType2 myType2;

I can replace @Mock with mock(MyType1.class).

But how can I replace @InjectMocks with a method call? Something like this:

injectMocks(MyType2.class)
  • 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-12T21:19:27+00:00Added an answer on June 12, 2026 at 9:19 pm

    Why using Autowired in your junit test? Since you are mocking the dependencies for MyType2 you must know its concreate implementation when you write your test.

    Then you don’t need and shouldn’t use Spring or any injection framework to create the instance of MyType2 that you want to test. Create it directly in your test initialization! I know that after some years of using IoC frameworks, it’s difficult to write myType2 = new MyType2Impl(mock(myType1.class)) but it will really makes your tests simpler, and faster (because no application context to build).
    E.g.:

    @Before
    public void setup() {
      myType1 = mock(MyType1.class);
      myType2 = new MyType2Impl(myType1);
    }
    

    But if you really want to use IoC in your junit tests, use springockito has suggested by Brice, and build your mock MyType1 in your application context.

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

Sidebar

Related Questions

I am looking for something like TRYCAST in TSQL or an equivalent method /
I'm trying to mock a method that has the equivalent of the following signature:
I'd like to call a static method via WMI using WQL (basically the equivalent
In MVC I can do something like the following to serialise an object with
I wonder if someone can help! I've been using following 'Get_Date' class with method
I'm trying to find the jQuery equivalent of this JavaScript method call: document.addEventListener('click', select_element,
How can I refresh a view within a controller method calling redirect (or equivalent)?
What is the extension method equivalent of the following LINQ? var qry = from
I want to do, in Perl, the equivalent of the following Ruby code: class
The following generic Guice binding method behaves correctly: <T> Key<?> bindMultibinder( ArrayList<Class<? extends T>>

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.