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

Spring--整合Junit

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

Spring--整合Junit

}

@Test

public void testDelete(){

//1.获取容器

ApplicationContext ac=new ClassPathXmlApplicationContext(“bean.xml”);

//2.得到业务层对象

IAccountService as=ac.getBean(“accountService”,IAccountService.class);

//3.执行方法

as.deleteAccount(4);

}

}

在上边代码中中我们可以看出,每一个测试模块中都有着重复的代码:

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-Xy4JzNIo-1652080908804)(https://img-blog.csdnimg.cn 《一线大厂Java面试题解析+后端开发学习笔记+最新架构讲解视频+实战项目源码讲义》无偿开源 威信搜索公众号【编程进阶路】 /20200910163227798.png#pic_left)]

采用Spring整合Junit:

相关配置:

  • 1. 导入spring整合junit的jar包(坐标)

  • 2. 使用Junit提供的一个注解把原有的main方法替换了,替换成spring提供的@Runwith

  • 3. 告知spring的运行器,spring和ioc创建是基于xml或注解的,并且说明位置

  • @ContextConfiguration:

Location:指定xml文件的位置,加上classpath关键字,表示在类路径下

classes:指定注解所在的位置

注意:当我们使用spring 5.x版本的时候,要求junit版本必须是4.12以上

1、在pom.xml中导入spring整合junit的jar包

xmlns:xsi=“http://www.w3.org/2001/XMLSchema-instance”

xsi:schemaLocation=“http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd”>

4.0.0

org.example

spring_day02_account_anno_withoutxml

1.0-SNAPSHOT

jar

org.springframework

spring-test

5.0.2.RELEASE

2、使用Junit提供的注解把原有的main方法替换

@RunWith(SpringJUnit4ClassRunner.class)

@ContextConfiguration(“classpath:bean.xml”)

//@ContextConfiguration(classes = SpringConfiguration.class)

public class AccountServiceTest2 {

@Autowired

private IAccountService as;

@Test

public void testFindAll(){

// //1.获取容器

// ApplicationContext ac=new ClassPathXmlApplicationContext(“bean.xml”);

// //2.得到业务层对象

// IAccountService as=ac.getBean(“accountService”,IAccountService.class);

//3.执行方法

List accounts=as.findAllAccount();

for(Account account:accounts){

System.out.println(account);

}

}

@Test

public void testFindOne(){

Account account=as.findAccountById(1);

System.out.println(account);

}

@Test

public void testSave(){

Account account=new Account();

account.setName(“sss”);

account.setMoney(123f);

as.saveAccount(account);

}

@Test

public void testUpdate(){

Account account=as.findAccountById(4);

account.setMoney(2131f);

as.updateAccount(account);

}

@Test

public void testDelete(){

as.deleteAccount(4);

}

void testUpdate(){

Account account=as.findAccountById(4);

account.setMoney(2131f);

as.updateAccount(account);

}

@Test

public void testDelete(){

as.deleteAccount(4);

}

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

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

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