Spring集成Junit

Spring集成Junit的步骤:

  1、导入spring集成Junit的坐标

<!--        spring集成Junit所需坐标-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>5.0.5.RELEASE</version>
</dependency>

  2、使用@Runwith注解替换原来的运行起期

    +-+在加载完坐标后创建一个测试类,在类外使用@Runwith来代替晚来的运行过程@RunWith(SpringJUnit4ClassRunner.class)

@RunWith(SpringJUnit4ClassRunner.class)

//@ContextConfiguration("classpath:applicationContext.xml")//使用配置文件
@ContextConfiguration(classes = {SpringConfig.class})
public class SpringJunit {
@Autowired
private UserService userService;

@Test
public void test(){
userService.save();
}
}


  3、使用@ContextConfiguration指定配置文件或配置类

    +-+在在@Runwith下面使用@ContextConfiguration来指定需要加载的配置文件或配置类

  4、使用@Autowire(@Resource)注入需要测试的对象

    将会帮我们自动的注入对象

  5、创建测试方法进行测试

    

posted @   孤巷一人i  阅读(85)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
点击右上角即可分享
微信分享提示