blue-sky
blue-sky

Reputation: 53806

How to autowire a Spring bean within a controller when testing?

I have a Spring test :

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:my-context.xml")
    public class MyTest {
        @Test
        public void testname() throws Exception {
           System.out.println(myController.toString());
        }

        @Autowired
        private MyController myController;
    }

This works fine when myController is defined in same class as MyTest but if I move MyController to another class it is not autowired, as running below returns null, so myController does not seem to be autowired correctly :

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:my-context.xml")
        public class MyTest {
            @Test
            public void testname() throws Exception {
               System.out.println(new TestClass().toString());
            }

        }

    @Controller
    public class TestClass {
            @Autowired
            private MyController myController;

            public String toString(){
              return myController.toString();       
            }
    }

Does autowiring only occur at within the class the test is being run from ? How can I enable autowiring on all classes that are instantiated by the test class ?

Update :

Thanks to the answers from smajlo & Philipp Sander I was able to fix this using this code to access this bean instead of creating the bean explicitly. This is already configured by Spring so I access it from the context :

ApplicationContext ctx = new ClassPathXmlApplicationContext("my-context.xml");  
TestClass myBean = (TestClass) ctx.getBean("testClass");  

When the bean is created explicitly it is not autowired by Spring.

Upvotes: 0

Views: 2230

Answers (2)

Philipp Sander
Philipp Sander

Reputation: 10249

As Sotirios Delimanolis already said:

MyTestClass needs to be managed by Spring to get autowired. to do this, simply at @Component to MyTestClass and autowire it

Upvotes: 0

smajlo
smajlo

Reputation: 972

new TestClass().toString()

If you creating object by manually invoking constructor obejct is not controlled by Spring so field won't be autowired.

EDIT:

maybe you want to create specific test-context and load both on test classes.. because for now i guess your way to do the testing is a little wrong. Why do you need from test class access to another test class? This is not unit test anymore:)

Your TestClass will be never autowired no matter what annotation you will add, because you creating new instance.. try this:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:my-context.xml")
    public class MyTest {
    @Autowired
    private TestClass testClass;
        @Test
        public void testname() throws Exception {
           System.out.println(testClass.toString());
        }

    }

@Controller
public class TestClass {
        @Autowired
        private MyController myController;

        public String toString(){
          return myController.toString();       
        }
}

Upvotes: 1

Related Questions