GVKun编程网logo

为什么 JUnit 不提供 assertNotEquals 方法?(为什么要用junit)

21

针对为什么JUnit不提供assertNotEquals方法?和为什么要用junit这两个问题,本篇文章进行了详细的解答,同时本文还将给你拓展Assert.assertEquals()方法参数详解、A

针对为什么 JUnit 不提供 assertNotEquals 方法?为什么要用junit这两个问题,本篇文章进行了详细的解答,同时本文还将给你拓展Assert.assertEquals()方法参数详解、Assert.assertEquals的使用方法及注意事项说明、assertEquals和assertNotEquals,谨慎使用 assertNotEquals、assertEquals(Double,Double)和assertEquals(double,double,delta)之间的Junit差异等相关知识,希望可以帮助到你。

本文目录一览:

为什么 JUnit 不提供 assertNotEquals 方法?(为什么要用junit)

为什么 JUnit 不提供 assertNotEquals 方法?(为什么要用junit)

有人知道为什么 JUnit 4 提供assertEquals(foo,bar)但不提供assertNotEqual(foo,bar)方法吗?

它提供assertNotSame(对应assertSame)和assertFalse(对应assertTrue),所以他们没有费心包括assertNotEqual.

顺便说一句,我知道 JUnit-addons 提供了我正在寻找的方法。我只是出于好奇而问。

答案1

小编典典

我建议您使用较新的assertThat()样式断言,它可以轻松描述各种否定,并自动构建对您的预期和断言失败时得到的结果的描述:

assertThat(objectUnderTest, is(not(someOtherObject)));assertThat(objectUnderTest, not(someOtherObject));assertThat(objectUnderTest, not(equalTo(someOtherObject)));

所有三个选项都是等效的,选择您认为最易读的一个。

要使用方法的简单名称(并允许这种时态语法起作用),您需要以下导入:

import static org.junit.Assert.*;import static org.hamcrest.CoreMatchers.*;

Assert.assertEquals()方法参数详解

Assert.assertEquals()方法参数详解

junit.framework包下的Assert提供了多个断言方法. 主用于比较测试传递进去的两个参数.

Assert.assertEquals();及其重载方法:

  • 1. 如果两者一致, 程序继续往下运行.
  • 2. 如果两者不一致, 中断测试方法, 抛出异常信息 AssertionFailedError .

 以Assert.assertEquals(int expected, int actual)为例:

/**
 * Asserts that two ints are equal. 断言两个int是相等的
 */
static public void assertEquals(int expected, int actual) {
    assertEquals(null, expected, actual);
}

可以看到里面调用了assertEquals(String message, int expected, int actual)方法:

/**
 * Asserts that two ints are equal. If they are not
 * an AssertionFailedError is thrown with the given message.
 * 如果不抛出带有 message 的异常(AssertionFailedError)信息, 则表明两者相等
 */
static public void assertEquals(String message, int expected, int actual) {
    assertEquals(message, Integer.valueOf(expected), Integer.valueOf(actual));
}

可以看到, 这里把int类型封箱成为Integer类型. 注释说, 会抛异常, 但这里没有. 没关系, 我们接着看里面调用: assertEquals(String message, Object expected, Object actual)方法:

/**
 * Asserts that two objects are equal. If they are not
 * an AssertionFailedError is thrown with the given message.
 * 如果不抛出带有 message 的异常(AssertionFailedError)信息, 则表明两者相等(这里比较的是Object对象)
 */
static public void assertEquals(String message, Object expected, Object actual) {
    if (expected == null && actual == null) {
        return;
    }
    if (expected != null && expected.equals(actual)) {
        return;
    }
    failNotEquals(message, expected, actual);
}

两个if语句, 判断了两者相等的情况: 引用(地址)相等或者内容相等. 如果这两种if情况都不命中, 那么表明1参和2参实际是不相等, 所以代码会往下执行failNotEquals(String message, Object expected, Object actual)方法,并在此方法中抛出异常, 接下来就比较简单了:

static public void failNotEquals(String message, Object expected, Object actual) {
    fail(format(message, expected, actual));
}

public static String format(String message, Object expected, Object actual) {
    String formatted = "";
    if (message != null && message.length() > 0) {
        formatted = message + " ";
    }
    return formatted + "expected:<" + expected + "> but was:<" + actual + ">";
}
/**
* Fails a test with the given message.
*/
static public void fail(String message) {
	if (message == null) {
	    throw new AssertionFailedError();
	}
	throw new AssertionFailedError(message);
}

以上可以看出, 最终是由fail(String message)这个方法抛出异常信息!!

Assert.assertEquals()使用方法:

使用, 示例代码:

Assert.assertEquals(true, arry.contains("hello"));
Assert.assertEquals(39991L, aa.getLong("key3", 0L));
Assert.assertEquals(true, bb.getBoolean("key4", false));
Assert.assertEquals(5.3f, cc.getFloat("key5", 0.f));
Assert.assertEquals(99, dd.getInt("key6", 1));
Assert.assertEquals("如果打印本信息, 证明参数不相等", 10L, 10);

按照源码分析, 我们可以把一个预期结果作为1参传递进去. 2参传递我们需要测试的方法. 然后执行. 相等, 代码继续往下执行, 不相等, 中断执行, 抛出异常信息!!!

略作一提:

Assert.assertSame(Object expected, Object actual)方法:

其比较的是引用地址是否相等, 并没有对内容进行比较:

/**
 * Asserts that two objects refer to the same object. If they are not
 * the same an AssertionFailedError is thrown.
 */
static public void assertSame(Object expected, Object actual) {
    assertSame(null, expected, actual);
}
/**
 * Asserts that two objects refer to the same object. If they are not
 * an AssertionFailedError is thrown with the given message.
 */
static public void assertSame(String message, Object expected, Object actual) {
    if (expected == actual) {
        return;
    }
    failNotSame(message, expected, actual);
}

到此这篇关于Assert.assertEquals()方法参数详解的文章就介绍到这了。希望对大家的学习有所帮助,也希望大家多多支持。

您可能感兴趣的文章:
  • 关于Assert.assertEquals报错的问题及解决
  • Java Assert.assertEquals案例详解
  • Assert.assertEquals的使用方法及注意事项说明

Assert.assertEquals的使用方法及注意事项说明

Assert.assertEquals的使用方法及注意事项说明

Assert.assertEquals使用及注意事项

在开发中,我们需要测试时候,不可能把全部程序运行一次,在此我们就需要通过编写单元测试来对程序进行测试了. 在Assert类里面有大量的静态方法,我们爱用的就是Assert.assertEquals这个静态方法,需要两个参数。

Assert运行环境需要什么导入什么jar包?

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

为什么需要两个jar包呢,因为我们在测试环境嘛

使用步骤

1.如果两个值相等代码演示

代码如下(示例):

String s="Hello Maven";
  Assert.assertEquals("Hello Maven",s);

正确情况下,不会有显示,会正常执行流程

如果错误

public class Demo2 {
    @Test
    public void Demotest(){
        //使用断言
        String s="Hello Maven";
        Assert.assertEquals("Hell Maven",s);
    }
}

在这里,我修改了值会有如下结果在控制台显示,不用看内容啊

E:\develop\bin\java.exe -ea -Didea.test.cyclic.buffer.size=1048576 “-javaagent:E:\develop\IntelliJ IDEA 2018.2.2\lib\idea_rt.jar=8413:E:\develop\IntelliJ IDEA 2018.2.2\bin” -Dfile.encoding=UTF-8 -classpath "E:\develop\IntelliJ IDEA 2018.2.2\lib\idea_rt.jar;E:\develop\IntelliJ IDEA 2018.2.2\plugins\junit\lib\junit-rt.jar;E:\develop\IntelliJ IDEA 2018.2.2\plugins\junit\lib\junit5-

at org.junit.Assert.assertEquals(Assert.java:125)
at org.junit.Assert.assertEquals(Assert.java:147)
at com.itheima.control.Demo2.Demotest(Demo2.java:13)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:566)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:263)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:69)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:48)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222)
at org.junit.runners.ParentRunner.run(ParentRunner.java:292)
at org.junit.runner.JUnitCore.run(JUnitCore.java:157)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)

Process finished with exit code -1

小结一下:对比可以是两个数据的值,也可以是两个数据的地址值,上面我们也演示了,当两个值正确情况下,测试模式可以正常运行,如果错误,会抛出异常,结束掉测试.

Assert.assertEquals是什么含义

assertEquals

函数原型1:

assertEquals([String message],expected,actual) 

参数说明: 

  • message是个可选的消息,假如提供,将会在发生错误时报告这个消息。 
  • expected是期望值,通常都是用户指定的内容。 
  • actual是被测试的代码返回的实际值。 

函数原型2:

assertEquals([String message],expected,actual,tolerance) 

参数说明: 

  • message是个可选的消息,假如提供,将会在发生错误时报告这个消息。 
  • expected是期望值,通常都是用户指定的内容。 
  • actual是被测试的代码返回的实际值。 
  • tolerance是误差参数,参加比较的两个浮点数在这个误差之内则会被认为是 相等的。

以上为个人经验,希望能给大家一个参考,也希望大家多多支持。

您可能感兴趣的文章:
  • 关于Assert.assertEquals报错的问题及解决
  • Assert.assertEquals()方法参数详解
  • Java Assert.assertEquals案例详解

assertEquals和assertNotEquals,谨慎使用 assertNotEquals

assertEquals和assertNotEquals,谨慎使用 assertNotEquals

在使用  org.testng.assert  中的

 

     assertEquals(long actual, long expected)   和  assertNotEquals(**,**) 的时候 注意 比较两端数据类型,

        

       在 Assert 类中提供的 

            assertEquals  支持:boolean double float  int long  Map  Object  byte[]  byte char  Coolection等        具体见API

         assertNotEquals  只支持 : float double Object 这三种类型 ,

         使用中 如果是不支持的数据类型   就会调用  Object  这种类型! 

            

                        例如:

    long long1 = 10L;
    long long2 = 10L;
// 以下 代码 全部通过,不会产生 assert 退出;
    Assert.assertEquals(long1, long2); //Assert.assertEqualslong,long)
    Assert.assertEquals(long1, 10); //Assert.assertEquals(long,long)
    Assert.assertNotEquals(long1, 10);//Assert.assertNotEquals(Object,Object)
    Assert.assertNotEquals(long2, 10); //Assert.assertNotEquals(Object,Object)
    
    Assert.assertNotEquals(long2, 9);//Assert.assertNotEquals(Object,Object)
登录后复制

 以上就是assertEquals和assertNotEquals,谨慎使用 assertNotEquals的内容。

assertEquals(Double,Double)和assertEquals(double,double,delta)之间的Junit差异

assertEquals(Double,Double)和assertEquals(double,double,delta)之间的Junit差异

我进行了一个junit测试,使用以下命令声明了两个Double对象:

Assert.assertEquals(Double expected,Double result);

很好,然后我决定将其更改为使用原始double,除非您也提供了增量,否则该结果被弃用了。

所以我想知道在assertEquals中使用Double对象还是原始类型有什么区别?为什么不使用不带增量的对象,但不推荐使用不带增量的基元呢?Java是否在后台执行了已经考虑了默认增量值的操作?

谢谢。

我们今天的关于为什么 JUnit 不提供 assertNotEquals 方法?为什么要用junit的分享就到这里,谢谢您的阅读,如果想了解更多关于Assert.assertEquals()方法参数详解、Assert.assertEquals的使用方法及注意事项说明、assertEquals和assertNotEquals,谨慎使用 assertNotEquals、assertEquals(Double,Double)和assertEquals(double,double,delta)之间的Junit差异的相关信息,可以在本站进行搜索。

本文标签: