Junit测试框架

简介: 最简单的例子介绍Junit测试框架 public class Person { public void run() { System.out.println("run!!!"); } public void eat() { System.out.println("eat!!!"); } public static void main(String[] args)

最简单的例子介绍Junit测试框架

public class Person {

	public void run() {
		 System.out.println("run!!!");
	}
	public void eat() {
		System.out.println("eat!!!");
	}
	public static void main(String[] args){
		System.out.println("miao");
	}
} 

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

//Person类的测试类
public class test {
	private Person p;
	 //@Before、@Test、@After 是关键  没有这个标志不能生成Junit测试
	@Before     
	public void before() {
		System.out.println("before");
		p = new Person();
	}
	@Test        
	public void testRun() {
		p.run();
	}
	@Test
	public void testEat() {
		p.eat();
	}
	@After        
	public void after() {
		System.out.println("after");
		p = null;
	}
}
输出:

before
run!!!
after
before
eat!!!
after

import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;

//Person类的测试类
public class test {
	 //@BeforeClass、@Test、@AfterClass 是关键  没有这个标志不能生成Junit测试
	@BeforeClass     
	public static void beforeClass() {
		System.out.println("before");		
	}
	@Test        
	public void testRun() {
		Person p = new Person();
		p.run();
	}
	@Test
	public void testEat() {
		Person p = new Person();
		p.eat();
	}
	@AfterClass        
	public static void afterClass() {
		System.out.println("after");
	}
}
输出:
before
run!!!
eat!!!
after

public class Person {

	public String run() {
		// System.out.println("run!!!");
		 return "1";
	}
	public void eat() {
		System.out.println("eat!!!");
	}
	public static void main(String[] args){
		System.out.println("miao");
	}
}

import org.junit.Assert;
import org.junit.Test;

//Person类的测试类
public class test {
	 // @Test  是关键  没有这个标志不能生成Junit测试
 
	@Test        
	public void testRun() {
		Person p = new Person();
		//p.run();
		//断言
		Assert.assertEquals("1",p.run()); //测试预期的结果和运行的结果是否一致(测试通过,绿条)
		Assert.assertEquals("2",p.run()); //测试预期的结果和运行的结果是否一致(测试不通过,红条)
                 //Assert还有很多类方法具体可查询api
	}
	@Test
	public void testEat() {
		Person p = new Person();
		p.eat();
	}
 
}


目录
相关文章
|
安全 Unix Linux
Metasploit Pro 4.22.7-2025050101 发布 - 专业渗透测试框架
Metasploit Pro 4.22.7-2025050101 发布 - 专业渗透测试框架
389 10
Metasploit Pro 4.22.7-2025050101 发布 - 专业渗透测试框架
|
安全 Ubuntu Linux
Metasploit Pro 4.22.7-2025042101 发布 - 专业渗透测试框架
Metasploit Pro 4.22.7-2025042101 (Linux, Windows) - 专业渗透测试框架
559 5
Metasploit Pro 4.22.7-2025042101 发布 - 专业渗透测试框架
|
Web App开发 人工智能 JavaScript
主流自动化测试框架的技术解析与实战指南
本内容深入解析主流测试框架Playwright、Selenium与Cypress的核心架构与适用场景,对比其在SPA测试、CI/CD、跨浏览器兼容性等方面的表现。同时探讨Playwright在AI增强测试、录制回放、企业部署等领域的实战优势,以及Selenium在老旧系统和IE兼容性中的坚守场景。结合六大典型场景,提供技术选型决策指南,并展望AI赋能下的未来测试体系。
|
人工智能 Java 测试技术
Spring Boot 集成 JUnit 单元测试
本文介绍了在Spring Boot中使用JUnit 5进行单元测试的常用方法与技巧,包括添加依赖、编写测试类、使用@SpringBootTest参数、自动装配测试模块(如JSON、MVC、WebFlux、JDBC等),以及@MockBean和@SpyBean的应用。内容实用,适合Java开发者参考学习。
1699 0
|
SQL 安全 Linux
Metasploit Pro 4.22.8-20251014 (Linux, Windows) - 专业渗透测试框架
Metasploit Pro 4.22.8-20251014 (Linux, Windows) - 专业渗透测试框架
727 1
Metasploit Pro 4.22.8-20251014 (Linux, Windows) - 专业渗透测试框架
|
Linux 网络安全 iOS开发
Metasploit Framework 6.4.95 (macOS, Linux, Windows) - 开源渗透测试框架
Metasploit Framework 6.4.95 (macOS, Linux, Windows) - 开源渗透测试框架
1461 1
Metasploit Framework 6.4.95 (macOS, Linux, Windows) - 开源渗透测试框架
|
安全 Linux 网络安全
Metasploit Pro 4.22.8-2025091701 (Linux, Windows) - 专业渗透测试框架
Metasploit Pro 4.22.8-2025091701 (Linux, Windows) - 专业渗透测试框架
681 2
Metasploit Pro 4.22.8-2025091701 (Linux, Windows) - 专业渗透测试框架
|
Linux 网络安全 iOS开发
Metasploit Framework 6.4.90 (macOS, Linux, Windows) - 开源渗透测试框架
Metasploit Framework 6.4.90 (macOS, Linux, Windows) - 开源渗透测试框架
766 1
Metasploit Framework 6.4.90 (macOS, Linux, Windows) - 开源渗透测试框架
|
Java 测试技术 数据库连接
【SpringBoot(四)】还不懂文件上传?JUnit使用?本文带你了解SpringBoot的文件上传、异常处理、组件注入等知识!并且带你领悟JUnit单元测试的使用!
Spring专栏第四章,本文带你上手 SpringBoot 的文件上传、异常处理、组件注入等功能 并且为你演示Junit5的基础上手体验
1330 3
|
安全 Linux 网络安全
Metasploit Framework 6.4.88 (macOS, Linux, Windows) - 开源渗透测试框架
Metasploit Framework 6.4.88 (macOS, Linux, Windows) - 开源渗透测试框架
831 0

热门文章

最新文章