@Autowiredは自動的にnullポインター例外を挿入します(問題はJUnitユニットテストで発生します)

JUnitテストを使用しました

それは次のように書かれています:

@Component
public class Test_SSM {
    
    
    @Autowired
    private UserService userService;
    
    @Test
    public void show() {
    
    
        System.out.println(userService);
    }
}

次に、testshowメソッドを呼び出します

出力がnull

このように変更するだけです。テストクラスの2行の注釈

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

おすすめ

転載: blog.csdn.net/qq_39906884/article/details/84592354