SpringBoot - SpringBoot整合Junit(test)
文章目录
SpringBoot整合Junit
创建新模块
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-jLeDk2Rz-1651402368205)(springboot.assets/image-20220501122652030.png)]](https://file.cfanz.cn/uploads/png/2022/05/01/11/E1fVX8101d.png)
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-ZPe1jsdS-1651402368207)(springboot.assets/image-20220501122729750.png)]](https://file.cfanz.cn/uploads/png/2022/05/01/11/b511Y31H72.png)
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-oa1rb6Yp-1651402368207)(springboot.assets/image-20220501122753631.png)]](https://file.cfanz.cn/uploads/png/2022/05/01/11/QMWT2N3921.png)
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-EvaLkClh-1651402368208)(springboot.assets/image-20220501122830296.png)]](https://file.cfanz.cn/uploads/png/2022/05/01/11/SM043ACE1a.png)
创建成功
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-FyiTlYuz-1651402368208)(springboot.assets/image-20220501122945794.png)]](https://file.cfanz.cn/uploads/png/2022/05/01/11/24Iea4QI02.png)
编辑pom文件
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-aIppJGnD-1651402368209)(springboot.assets/image-20220501123112587.png)]](https://file.cfanz.cn/uploads/png/2022/05/01/11/DJE99L71Ga.png)
运行测试类
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-jVznDTd8-1651402368210)(springboot.assets/image-20220501123507020.png)]](https://file.cfanz.cn/uploads/png/2022/05/01/11/E9L44a69Cd.png)
模拟dao层
package com.taotao.dao;
@SuppressWarnings({"all"})
public interface BookDao {
public void save();
}
package com.taotao.dao.impl;
import com.taotao.dao.BookDao;
import org.springframework.stereotype.Repository;
@Repository
@SuppressWarnings({"all"})
public class BookDaoImpl implements BookDao {
@Override
public void save() {
System.out.println("book dao is running....");
}
}
测试dao层
package com.taotao.springboot_04_junit;
import com.taotao.dao.BookDao;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
class Springboot04JunitApplicationTests {
@Autowired
private BookDao bookDao;
@Test
void contextLoads() {
System.out.println("springboot text run...");
bookDao.save();
}
}
成功测试
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-Y5xGkTj9-1651402368211)(springboot.assets/image-20220501125107507.png)]](https://file.cfanz.cn/uploads/png/2022/05/01/11/Yd7f6ETDWM.png)
小结
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-TOJrIg9J-1651402368211)(springboot.assets/image-20220501125155161.png)]](https://file.cfanz.cn/uploads/png/2022/05/01/11/066KED6047.png)
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-4fMtsxSN-1651402368212)(springboot.assets/image-20220501125201824.png)]](https://file.cfanz.cn/uploads/png/2022/05/01/11/44QQ53O0f1.png)
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-hGSWEwnT-1651402368213)(springboot.assets/image-20220501125207479.png)]](https://file.cfanz.cn/uploads/png/2022/05/01/11/dI0DaNSM10.png)
classes属性
遇到问题
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-LOXPx1wS-1651402368214)(springboot.assets/image-20220501184633590.png)]](https://file.cfanz.cn/uploads/png/2022/05/01/11/97T787NSb9.png)
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-gk7eEqF2-1651402368214)(springboot.assets/image-20220501184643157.png)]](https://file.cfanz.cn/uploads/png/2022/05/01/11/8af063YPJH.png)
解决方案1
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-tyFyCwth-1651402368215)(springboot.assets/image-20220501184802258.png)]](https://file.cfanz.cn/uploads/png/2022/05/01/11/bbZM5T9V20.png)
测试成功
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-mVdU5wN8-1651402368215)(springboot.assets/image-20220501184828790.png)]](https://file.cfanz.cn/uploads/png/2022/05/01/11/EJc8TY9703.png)
解决方案2
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-tGJT5m72-1651402368215)(springboot.assets/image-20220501185010213.png)]](https://file.cfanz.cn/uploads/png/2022/05/01/11/82XWVYddaD.png)
测试成功
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-5B3xWjfL-1651402368216)(springboot.assets/image-20220501185059503.png)]](https://file.cfanz.cn/uploads/png/2022/05/01/11/0954f07Z07.png)
小结
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-UL794ljx-1651402368216)(springboot.assets/image-20220501185220368.png)]](https://file.cfanz.cn/uploads/png/2022/05/01/11/5a6CGEfQfW.png)