如何修改TestNG assertEquals? [英] How to modify TestNG assertEquals?

查看:166
本文介绍了如何修改TestNG assertEquals?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有实际和预期的对象,每个对象都包含一些数据成员.对于一个数据成员,我需要执行一个包含检查,而不是等于检查,对于其余数据,等于检查已经完成.有办法吗?

I have actual and expected objects each containing some data members. For one data member, I need to do a contains check instead of equals check and for the rest, equals check is done. Is there a way to do this ?

推荐答案

不是隐式的,但是您至少具有以下两个选项:

Not implicitly, but you have at least the following 2 options:

  • use TestNG's assertTrue
  • use an additional library such as Hamcrest, AssertJ, etc

依赖项:

    <dependency>
        <groupId>org.hamcrest</groupId>
        <artifactId>hamcrest-core</artifactId>
        <version>1.3</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.assertj</groupId>
        <artifactId>assertj-core</artifactId>
        <version>3.9.0</version>
        <scope>test</scope>
    </dependency>

代码:

import org.testng.annotations.Test;

import static org.assertj.core.api.Assertions.assertThat;
import static org.hamcrest.CoreMatchers.*;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.testng.Assert.assertTrue;

public class MyTest {

    @Test
    public void shouldCheckStringContains() {
        String myString = "This is my string";

        // TestNG
        assertTrue(myString.startsWith("This") 
                && myString.contains("is") 
                && myString.contains("my") 
                && myString.endsWith("string"));

        // Hamcrest
        assertThat(myString, allOf(startsWith("This"),
                                   containsString("is"),
                                   containsString("my"),
                                   endsWith("string")));

        //AssertJ
        assertThat(myString).startsWith("This")
                            .contains("is")
                            .contains("my")
                            .endsWith("string");
    }
}

这篇关于如何修改TestNG assertEquals?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆