世界那么好,机会那么多

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

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 @   面向对象爱好者社区  阅读(1206)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具
历史上的今天:
2021-07-09 windows10 64位操作系统 安装oracle11g 客户端 时出现了:[INS-13001]环境不满足最低要。
点击右上角即可分享
微信分享提示