@Autowired objects getting null value

后端 未结 6 1879
情话喂你
情话喂你 2021-01-12 04:29

Trying to set up a project but fail at Autowiring objects through Spring.

package se.hsr.web;

public class TestRunner {

    public static void main(String[         


        
6条回答
  •  礼貌的吻别
    2021-01-12 04:51

    You need this at the top of your test class:

    @RunWith(SpringJUnit4ClassRunner.class)
    // ApplicationContext will be loaded from "/applicationContext.xml" and "/applicationContext-test.xml"
    // in the root of the classpath
    @ContextConfiguration(locations={"/applicationContext.xml", "/applicationContext-test.xml"})
    public class MyTest {
    

    I assumed JUnit4; my oversight.

    You do need the context configuration tag in an application context somewhere, but I don't see anyplace in your code where you're actually opening an application context file and creating an ApplicationContext. Usually that's done in a set up method for your test. You'll have better luck if you actually create an ApplicationContext somewhere. Try reading the XML from your CLASSPATH in a setup method and see if that helps.

提交回复
热议问题