专业的编程技术博客社区

网站首页 > 博客文章 正文

使用springboot工程进行测试时出现bean未注册到容器里面

baijin 2024-08-20 10:26:25 博客文章 6 ℃ 0 评论

随手小记

测试时使用AutoWired注解注入bean,在debug调试时发现注入到bean都是为null,导致报空指针异常,网上查了资料,大家都说得含糊其辞,寻同事的帮助,最后得以解决,
添加 @RunWith(SpringRunner.class) 实现在加载该测试类的时候也能在容器中提取到bean


@Slf4j
@SpringBootTest
@RunWith(SpringRunner.class)
public class DesentisitionTest {

    @Autowired
    private Function function;

    @Test
    public void sendMail() throws Exception {

				Map<String,Obj> param = new HashMap<>();
        function.process(param);
        log.info("脱敏测试")

    }
}
  • 添加注解后,能拿到对应的bean

记录一下,便于以后测试的时候使用再出现这类问题

其实这些是技能问题,说到底还是代码敲少了,继续累加代码量

本文暂时没有评论,来添加一个吧(●'◡'●)

欢迎 发表评论:

最近发表
标签列表