1

I am getting a NullPointerException while testing Spring Junit test configurations. The issue is with using the @ContextConfiguration and the @Autowired annotation.

When I instantiate the context and get a reference to the bean directly, as shown with the commented out code in the test method, the test runs correctly and succeeds. But when I try to use the @ContextConfiguration and @autowired annotations, with the same XML file attribute I get a NullPointerException in my assertEquals statement. Do you have any idea what I'm doing wrong?

package com.greathouse.helloworld.HelloWorldTest;

import javax.inject.Inject;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import junit.framework.TestCase;

@ContextConfiguration(locations = {"classpath:/resources/application-config.xml"})
@RunWith(SpringJUnit4ClassRunner.class)
public class AppTest extends TestCase
{
    @Autowired
    MessageService messageService;

    @Test
    public void testApp()
    {
        //ApplicationContext context = new ClassPathXmlApplicationContext("classpath:/resources/application-config.xml");
        //messageService = context.getBean("printMessage", MessageService.class);
        assertEquals( messageService.getMessage(), "Hello World" );
    }
}

2 Answers 2

7

It looks like messageService did not get autowired. It would help if you put messageService as required.

@Autowired( required = true )

This way when spring context starts spring will tell you why it didn't autowire your component. Also as a side note, since you are using JUnit 4 your test does not need to extend from TestCase.

Sign up to request clarification or add additional context in comments.

3 Comments

Thank you, that fixed it. Now I wonder why the Core Spring documentation didn't state that the required=true attribute was needed. Also thank you for clarify the extension of TestCase, Maven did that for me. But it's good to know what is needed and what's superfluous.
The attribute require=true is not mandatory, but it's nice to use it when you are sure that your dependency must be resolved.
I thought it is set to required = true by default?
0

Your field messageService isn't initialized - You should uncomment the context.getBean-line again.

1 Comment

That would not achieve what I want. The @Autowired annotation does the initialization. It declares to the container to initialize that object with the declared bean matching that type.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.