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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T12:31:07+00:00 2026-05-29T12:31:07+00:00

I am using spring 3 mvc/security frameworks. I have created a Controller class that

  • 0

I am using spring 3 mvc/security frameworks.

I have created a Controller class that has a reference to a repository to load data from. The class is annotated with @Controller, the repository class is annotated with @Repository, and the instance of the respository is @Autowired.

However when I try to unit test, the autowired instance throws a null pointer exception.

Now, I understand that because it is autowired, it needs to be within the spring context to be picked up. But I feel that if I use @RunsWith() then it becomes an integration test. I would really like to separate the integration tests (using @RunsWith) and the unit test for this method. Any ideas on how I can get around this null pointer exception? Would just creating getter/setter methods on my controller class be okay?:

Repository class:

@Repository
public class Repository{
 ....
}

Controller class:

@Controller
public class Controller{
@Autowired
private Repository repo;
....
public String showView(){
    repo.doSomething();
}

Test class:

public ControllerTest {
@Test
public shouldDoTestOfShowView(){
}
}
  • 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-29T12:31:08+00:00Added an answer on May 29, 2026 at 12:31 pm

    I personally tend to use @Simon’s approach, rather than exposing setters, although either approach is fine. Adding setters just for the sake of tests is a bit annoying, though.

    An alternative is to use Spring’s ReflectionTestUtils class to directly poke dependencies into the field using reflection, removing the need for special constructors and setters, e.g.

    public ControllerTest {
       @Test
       public shouldDoTestOfShowView() {
          Controller controller = new Controller();
          Repository repository = new Repository();
    
          ReflectionTestUtils.setField(controller, "repo", repository); 
       }
    }
    

    Whether or not that still constitutes an “integration test” is your call (I don’t).

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

Sidebar

Related Questions

we're using Spring MVC with Spring security. One of the requirements is that if
has any one using Spring MVC with Jquery! i have got a strange problem
We have a Spring web application created using Spring MVC 3.0 In the same
i have written a small webapp using spring-security and spring-mvc with an annotation based
I have a Spring 3 MVC website using Spring Security 3.1.0RC2. Currently I am
I am using spring mvc and spring security. In my security-app-context.xml I have: <authentication-manager>
I have a Spring MVC app that does not protect updates of user data
I am using Spring MVC to build my web application, and I have a
I'm using Spring's form tag library in a Spring MVC application that I am
See the following Mock Test by using Spring/Spring-MVC public class OrderTest { // SimpleFormController

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.