Junit - 断言方法(Assert Methods)

Junit 4 断言方法允许检查测试方法的期望结果值和真实返回值。Junit的org.junit.Assert类提供了各种断言方法来写junit测试。这些方法被用来检查方法的真实结果值和期望值。下列一些有用的断言方法列表:

Junit 4 Assert Methods
MethodDescription
assertNull(java.lang.Object object)检查对象是否为空
assertNotNull(java.lang.Object object)检查对象是否不为空
assertEquals(long expected, long actual)检查long类型的值是否相等
assertEquals(double expected, double actual, double delta)检查指定精度的double值是否相等
assertFalse(boolean condition)检查条件是否为假
assertTrue(boolean condition)检查条件是否为真
assertSame(java.lang.Object expected, java.lang.Object actual)检查两个对象引用是否引用同一对象(即对象是否相等)
assertNotSame(java.lang.Object unexpected, java.lang.Object actual)

检查两个对象引用是否不引用统一对象(即对象不等)

 Junit 4断言方法样例

AssertionsTest.java junit测试用例,显示各种断言方法:

import static org.junit.Assert.*;
import java.util.ArrayList;
import java.util.List;
import org.junit.Test;
 
/**
 * @author javatutorials.co.in
 */
public class AssertionsTest {
 
    @Test
    public void testAssertNull() {
        String str = null;
        assertNull(str);
    }
 
    @Test
    public void testAssertNotNull() {
        String str = "hello Java!!";
        assertNotNull(str);
    }
 
    @Test
    public void testAssertEqualsLong() {
        long long1 = 2;
        long long2 = 2;
        assertEquals(long1, long2);
    }
 
    @Test
    public void testAssertEqualsDouble() {
        // test case is successfull as double1 and double 2
        // differ by 0.001 which is less than our specified delta
        double double1 = 1.236;
        double double2 = 1.237;
        double delta = 0.002;
        assertEquals(double1, double2, delta);
    }
 
    @Test
    public void testAssertTrue() {
        List<String> list = new ArrayList<String>();
        assertTrue(list.isEmpty());
    }
 
    @Test
    public void testAssertFalse() {
        List<String> list = new ArrayList<String>();
        list.add("hello");
        assertFalse(list.isEmpty());
    }
 
    @Test
    public void testAssertSame() {
        String str1 = "hello world!!";
        String str2 = "hello world!!";
        assertSame(str2, str1);
    }
 
    @Test
    public void testAssertNotSame() {
        String str1 = "hello world!!";
        String str3 = "hello Java!!";
        assertNotSame(str1, str3);
    }
}

样例输出

在eclipse Junit 窗口的输出如下:

附赠:






  1. Junit - 测试框架介绍
  2. Junit - Eclipse 教程
  3. Junit - 基础注解(@BeforeClass、@Before、@Test、@After、@AfterClass)
  4. Junit - 断言方法(Assert Methods)
  5. Junit - 参数化测试(Parameterized Test)
  6. Junit - 套件测试(Suite Test)
  7. Junit - 忽略测试(Ignore Test)
  8. Junit - 超时测试(Timeout Test)
  9. Junit - 期望异常测试(Expected Test)
  10. Junit - 优先级测试(FixMethodOrder Test)
  • 7
    点赞
  • 34
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
JUnit-BOM(Bill of Materials)是JUnit团队为JUnit 5框架提供的一个特殊模块。它的目的是简化JUnit 5的依赖管理。通过添加JUnit-BOM到你的项目中,你可以使用JUnit 5的所有核心模块和扩展模块,而无需单独指定每个模块的版本。 要使用JUnit-BOM,你需要在你的项目的构建文件中添加一个依赖项。对于Maven项目,可以在`<dependencies>`标签中添加以下内容: ``` <dependency> <groupId>org.junit</groupId> <artifactId>junit-bom</artifactId> <version>5.x.x</version> <!-- 替换为你想要使用的JUnit 5版本 --> <type>pom</type> <scope>import</scope> </dependency> ``` 对于Gradle项目,可以在`dependencies`块中添加以下内容: ``` dependencies { testImplementation platform('org.junit:junit-bom:5.x.x') // 替换为你想要使用的JUnit 5版本 testImplementation 'org.junit.jupiter:junit-jupiter-api' // 添加其他所需的JUnit 5模块 } ``` 添加JUnit-BOM之后,你可以使用JUnit 5的各种功能,例如编写和运行JUnit单元测试。你可以使用`@Test`注解来标记测试方法,并使用JUnit提供的各种断言和注解来编写强大的单元测试。 通过引用JUnit-BOM,你可以确保你的项目使用与JUnit 5兼容的版本,并且可以轻松管理依赖关系。这使得在不同的项目中使用JUnit 5变得更加简单和一致。 JUnit-BOM的详细信息可以在JUnit官方文档中找到。 JUnit是一个用于编写和运行可重复自动化测试的Java单元测试框架。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

放羊的牧码

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值