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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T15:15:35+00:00 2026-06-13T15:15:35+00:00

I have a spring controller with multiple spring bean dependencies (autowired services). Each service

  • 0

I have a spring controller with multiple spring bean dependencies (autowired services). Each service has also few spring bean dependencies (autowired daos).
For instance, controller login method:

@Controller
@RequestMapping("/")
public class ClientAccessController extends BaseController {

    @Autowired
    IFileService fileService;

    @Autowired
    MidTierService midTierService;

    /**
     * Used to handle client login requests
     * Works as a proxy to MID tier server
     *
     * @param request LoginRequest(userLogin, userPassword, compId, installGuid)
     * @return LoginResponse (token)
     * @throws Exception
     */
    @RequestMapping(method = RequestMethod.POST, value = "/login", headers = "Content-   type=application/json")
    public ResponseEntity<LoginResponse> login(@RequestBody LoginRequest request) throws Exception {

        log.info("LOGIN REQUEST [ " + request.toString() + " ]");

        String token = midTierService.authenticateNativeClient(request.getLogin(), request.getPassword(), request.getGuId(),
                request.getCompid().toString());
        LoginResponse response = new LoginResponse(token);

        HttpHeaders httpHeaders = new HttpHeaders();
        httpHeaders.setContentType(MediaType.APPLICATION_JSON);

        ResponseEntity<LoginResponse> responseEntity = new ResponseEntity<>(response, HttpStatus.OK);

        log.info("LOGIN RESPONSE [ " + response.toString());

        return responseEntity;
    }

I am trying to write JUnit tests for only controller ClienAccessController. But when I autowire ClientAccessController in test class, spring tries to create beans for all controller dependencies and all nested ones (daos etc) but I really don’t need em, only MidTierService.
So what should I do to exclude nested beans initializations and to use only what I need in test classes?

  • 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-13T15:15:37+00:00Added an answer on June 13, 2026 at 3:15 pm

    Spring allows more than one way to @Autowired members. The perferred approach for me (from a testability standpoint) is to @Autowired the constructor. So I would create a constructor for ClientAccessController that looks like this:

    @Autowired
    public ClientAccessController(final IFileService fileService, final MidTierService midTierService) {
        this.fileService = fileService;
        this.midTierService = midTierService;
    }
    

    If your autowiring is a bit more complex (in that it uses @Qualifier or @Value) more can be done with parameter annotations.

    @Autowired
    public ClientAccessController(@Qualifier("myFileService") final IFileService fileService,
           @Qualifier("myMidTierService") final MidTierService midTierService) {
        this.fileService = fileService;
        this.midTierService = midTierService;
    }
    

    That way, you can easily set up this class with mock objects or stubs for testing.

    @Test
    public void testClientAccessController() {
        final IFileService mockFileService = Mockito.mock(IFileService.class);
        final MidTierService mockMidTierService = Mockito.mock(MidTierService.class);
        final ClientAccessController controller = new ClientAccessController(mockFileService, mockMidTierService);
        //test it
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is it possible to have one controller to handle multiple forms in spring? For
I have a Spring controller that returns XHTML, what's the easiest way to setup
If I have a Spring Controller with two SEPARATE methods, one annotated by: @ExceptionHandler(Exception.class)
I have a controller (Spring-MVC) that generates a PDF report on a get request.
I have a spring action that I am rendering some json from the controller,
Just wondering if there is a way in Spring to have a parent controller:
I have a doubt about Model behavior in Spring MVC. I have a controller
I'm facing following problem actually: I'm using Spring with jQuery. I have Controller: @Controller
I have a multiple select variable posting to controller. The way multiple select works
I have this controller which performs me a multiple domain databinding class, and it

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.