idea使用junit测试
引入依赖
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<!--scope为test表示依赖项目仅仅参与测试相关的工作,包括测试代码的编译-->
<!--我们要进行项目的某部分的test测试,会放在名为test的文件夹下-->
<!--而现在我们的文件夹测试并不是test下面的一个测试,所以如果我们希望在别的地方也可以利用test注解,只需要去掉scope这一行就行了-->
<!--<scope>test</scope>-->
</dependency>
demo
package com.xie.util;
import org.junit.Assert;
import org.junit.Test;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import java.io.IOException;
import java.io.InputStream;
/**
* @Description ClassPathResource加载资源文件用法
* @Date 2022-04-02 14:38
* @Author xie
*/
public class ClassPathResourceTest {
@Test
public void test() throws IOException {
Resource res = new ClassPathResource("ehcache-shiro.xml");
res.getFile();
InputStream input = res.getInputStream();
Assert.assertNotNull(input);
}
@Test
public void test1() {
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
// ClassLoader.getResource("")获取的是classpath的根路径
System.out.println("a--- " + classLoader.getResource("").getPath());
// ClassLoader.getResource("")获取的是整个根路径
System.out.println("b--- " + new ClassPathResource("/").getPath());
System.out.println("b-1--- " + new ClassPathResource("").getPath());
// Class.getResource("")获取的是相对于当前类的相对路径
System.out.println("c--- " + this.getClass().getResource("").getPath());
// Class.getResource("/")获取的是classpath的根路径
System.out.println("d--- " + this.getClass().getResource("/").getPath());
}
}
最后附上一些使用到的概念:
一个测试类中只能声明此注解一次,此注解对应的方法只能被执行一次
@BeforeClass
使用此注解的方法在测试类被调用之前执行
@AfterClass
使用此注解的方法在测试类被调用结束退出之前执行
一个类中有多少个@Test注解方法,以下对应注解方法就被调用多少次
@Before
在每个@Test调用之前执行
@After
在每个@Test调用之后执行
@Test
使用此注解的方法为一个单元测试用例,一个测试类中可多次声明,每个注解为@Test只执行一次
@Ignore
暂不执行的测试用例,会被JUnit4忽略执行
欢迎一起来学习和指导,谢谢关注!
本文来自博客园,作者:xiexie0812,转载请注明原文链接:https://www.cnblogs.com/mask-xiexie/p/16093340.html
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· DeepSeek 开源周回顾「GitHub 热点速览」
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了