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.


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();       
        }
}

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

链接地址: http://www.djcxy.com/p/67728.html

上一篇: 将Spring XML文件转换为Spring @Configuration类

下一篇: 测试时如何在控制器中自动装入Spring bean?