我用的Junit測試
寫法如下:
@Component
public class Test_SSM {
@Autowired
private UserService userService;
@Test
public void show() {
System.out.println(userService);
}
}
然後調用測試show方法
輸出為 null
隻需這樣改, 測試類上的兩行注解
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration( locations = "classpath:applicationContext.xml")
/*locations屬性值 為 spring 配置檔案路徑*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration( locations = "classpath:applicationContext.xml")
public class Test_SSM {
@Autowired
private UserService userService;
@Test
public void show() {
System.out.println(userService);
}
}