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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T13:17:44+00:00 2026-05-27T13:17:44+00:00

I´m having issues with my test cases after having introduced @Autowired in one of

  • 0

I´m having issues with my test cases after having introduced @Autowired in one of the classes under test.

My testcase now looks like this:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={"/applicationContext.xml", "/spring-security.xml"})
public class StudentRepositoryTest extends AbstractDatabaseTestCase {

private StudentRepository studentRepository;
private CompanyRepository companyRepository;
private Student testStudent;
private Company testCompany;

@Before
public void setUp() {
    studentRepository = new StudentRepository();
    studentRepository.setJdbcTemplate(getJdbcTemplate());
    testStudent = Utils.testStudentNoApplication();
}
@Test
....

}

StudentRepository now looks like this:

@Service
public class StudentRepository extends AbstractRepository<Student> {

...

private PasswordEncoder passwordEncoder;
private MailService mailService;

public StudentRepository() {
    // TODO Auto-generated constructor stub
}

@Autowired 
public StudentRepository(MailService mailService, PasswordEncoder passwordEncoder) {
    this.mailService = mailService;
    this.passwordEncoder = passwordEncoder;
}

Obviously this test case won´t work anymore.
But what changes do I need to make to the testcase for the @Autowired annotation to be picked up by the test case?

EDIT:

I´ve now updated my setUp() to this (I need the password encoder to avoid null password):

@Before
public void setUp() {
    //studentRepository = new StudentRepository();
    studentRepository = new StudentRepository(mock(MailService.class), ctx.getAutowireCapableBeanFactory().createBean(ShaPasswordEncoder.class));
    studentRepository.setJdbcTemplate(getJdbcTemplate());
    testStudent = Utils.testStudentNoApplication();
}

My testcase is now running OK, but my testsuite failes with a NullPointerException.
I´m guessing the ApplicationContext is not being Autowired when running the testsuite for some reason?

  • 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-27T13:17:45+00:00Added an answer on May 27, 2026 at 1:17 pm

    If you don’t want to declare your StudentRepository in one of XML files referenced by @ContextConfiguration and autowire it into the test, you can try to use AutowireCapableBeanFactory as follows:

    ...
    public class StudentRepositoryTest extends AbstractDatabaseTestCase {
        ...
        @Autowired ApplicationContext ctx;
    
        @Before
        public void setUp() {
            studentRepository = ctx.getAutowireCapableBeanFactory()
                                   .createBean(StudentRepository.class);
            ...
        }
        ...
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am having issues getting the -e and -d file test operators to work
We've been having some issues with a SharePoint instance in a test environment. Thankfully
I'm having rewrite issues. http://test.bradp.com/drupal/Crocodile-Style/1 works OK. http://test.bradp.com/drupal/Crocodile-Style/ DOES NOT WORK. Apache throws a
Having issues referencing $(this) from within a the nested ajax 'success' function... I know
Still having issues with this problem. Please help if you can. So I am
Im having issues getting this to work, maybe its not even possible? I have
Im having issues vertically positioning text inside of a text input field in Firefox.
I'm having issues with my SQL Reporting Services reports. I'm using a custom font
I'm having issues creating an ActionLink using Preview 5. All the docs I can
I'm having issues with color matching css background colors with colors in images on

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.