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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T10:29:15+00:00 2026-05-21T10:29:15+00:00

I have a spring application and I want to create a unitary test on

  • 0

I have a spring application and I want to create a unitary test on a controller like this one. The problem is that the Wrapper class is a private inner class, so Wrapper is not understood in the test. Is it possible to mock it with Mockito without changing the controller class. I can use prepareData() to get an instance of the object, but I don’t know if this could be used to mock that object.

Thanks

@Controller
public class Controller {

    private class Wrapper {
        private Object1 field1;
        private Object2 field2;
        private Object1 method1(){
           ...
        }
        private Object2 method1(){
           ...
        }
    }

    @ModelAttribute("data")
    public Wrapper prepareData() {
            return new Wrapper ();
}

    public String save(@ModelAttribute("data") Wrapper wrapper, BindingResult result, Model model){
        ...
    }
}

So in my test I would have something like this

@Test
public void usernameEmpty(){

    BindingResult result = Mockito.mock(BindingResult.class);
    Model model = Mockito.mock(Model.class);
    Wrapper data = //how to mock it
    when(data.method1()).then(new Foo1());
    when(data.method2()).then(new Foo2());
    String returned = controller.save(data, result, model);
    ....
}
  • 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-05-21T10:29:15+00:00Added an answer on May 21, 2026 at 10:29 am

    Your test is on methods, but it tests the whole class behavior. If your inner class is private then its an implementation detail. Something that the test shouldn’t know. It there’s a lot of behaviour in that inner-class and you want to test it independently maybe you should make it public and separate from this class.

    Maybe you think: but then… it’s a lot of code to test (a very big indivisible thing), can’t I test something smaller? Well… yes. Test Driven Development mandates to make a minimal implementation and add more code only if you add more tests. So you start with some test and minimal implementation and evolve both of them until the tests have all the specification and the code all the implementation.

    So don’t worry about private inner classes. Test your class contract!

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

Sidebar

Related Questions

Hi i want to create an application that works like spring quartz i can
I have a Spring application that I believe has some bottlenecks, so I'd like
We have a Hibernate/Spring application that have the following Spring beans: <bean id=transactionManager class=org.springframework.orm.hibernate3.HibernateTransactionManager
I have a Spring Web MVC application that I'd like to serve a large,
In my spring application context file, I have something like: <util:map id="someMap" map-class="java.util.HashMap" key-type="java.lang.String"
We have a web based java/JSF/Spring application for which we want to create sitempas.xml
I want to create simple Spring MVC Hello world application that runs under Jetty
I have a j2ee web application running on Spring framework. I want to implement
I have a spring application that uses an embedded Jetty instance. Since I am
I'm trying to make this 2-player web game application using Spring MVC. I have

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.