世界那么好,机会那么多

这里除了干货,什么都没有

springboot中使用Test过程使用@Autowired注入为空

/**
 * 1、想要远程调用别的服务
 *  1)引入open-feign依赖
 *  2)编写一个接口,告诉springcloud这个接口需要调用远程服务
 *      1、声明接口的每一个方法都是调用哪个远程服务的哪个请求
 *      2、开启远程调用功能
 */
@EnableFeignClients(basePackages = "com.xinyu.feign")
@SpringBootApplication
@EnableDiscoveryClient
@MapperScan("com.xinyu.mapper")
public class UserServiceApplication {

    public static void main(String[] args) {
        SpringApplication.run(UserServiceApplication.class, args);
        System.out.println("user service application start...");
    }

}

这个代码可以看看是否注入成功,注入成功然后在使用的时候controller层可以使用但是在Test不可以

那样就在test类中加

@RunWith(SpringJUnit4ClassRunner.class)

这样就不会null了

@SpringBootTest
@RunWith(SpringJUnit4ClassRunner.class)
public class SampleTest {

    @Resource
    private UserMapper userMapper;

    @Test
    public void testSelect() {
        System.out.println(("----- selectAll method test ------"));
        List<User> userList = userMapper.selectList(null);
        Assert.assertEquals(5, userList.size());
        userList.forEach(System.out::println);
    }

}

 

posted @ 2022-07-09 18:34  面向对象爱好者社区  阅读(1271)  评论(0)    收藏  举报