栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

【无标题】

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

【无标题】

Spring对Junit的整合中 @Runwith的使用

Spring 对Junit整合要加@Runwith注解 表明是在Spring的环境下运行,这样才能获取到容器中的对象 不报空指针


Springboot 不用加@Runwith注解 即可运行
@SpringBootTest
class BootDemoApplicationTests {
	@Autowired
	private RoomMapper roomMapper
	@Test
    void testRoom(){
        List rooms = roomMapper.selectAll();
        System.out.println(rooms);
    }
}


原因分析:

1.Junit的版本问题
Spring 中整合的是Junit4 所以需要加@Runwith注解
Springboot 中 整合的是Junit5 ,在Junit5中 @Runwith注解被@ExtendWith注解代替了

2.@SpringBootTest
Springboot中添加的@SpringBootTest 注解已经定义了 @ExtendWith的默认值,所以连@ExtendWith也不用添加了。

@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@BootstrapWith(SpringBootTestContextBootstrapper.class)
@ExtendWith({SpringExtension.class})
public @interface SpringBootTest {
......
}
参考文章:

1.https://www.jianshu.com/p/4648fd55830e
2.还有一篇说了许多注解都被@SpringbootTest注解替换了 不用写,找不到了。


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/891847.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号