Spring boot test测试中@Autowired不起作用

1,问题:如下所示:

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

public class MoveSysUserTest {
    @Autowired
    private MoveSysUser moveSysUser;

    @Test
    public void testMoveSysUser() {
        boolean res = moveSysUser.moveSysUser();
        System.out.println(res);
    }
}

此时运行,会提示moveSysUser为空,报空指针异常

2,解决方法

1)添加注解:

@RunWith(SpringRunner.class)
@SpringBootTest
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

@RunWith(SpringRunner.class)
@SpringBootTest
public class MoveSysUserTest {
    @Autowired
    private MoveSysUser moveSysUser;

    @Test
    public void testMoveSysUser() {
        boolean res = moveSysUser.moveSysUser();
        System.out.println(res);
    }
}
2)继承一个添加了以上注解的类

猜你喜欢

转载自blog.csdn.net/qq_2300688967/article/details/80054766