Assert.assertEquals的使用方法及注意事項(xiàng)說(shuō)明
Assert.assertEquals使用及注意事項(xiàng)
在開發(fā)中,我們需要測(cè)試時(shí)候,不可能把全部程序運(yùn)行一次,在此我們就需要通過(guò)編寫單元測(cè)試來(lái)對(duì)程序進(jìn)行測(cè)試了. 在Assert類里面有大量的靜態(tài)方法,我們愛用的就是Assert.assertEquals這個(gè)靜態(tài)方法,需要兩個(gè)參數(shù)。
Assert運(yùn)行環(huán)境需要什么導(dǎo)入什么jar包?
import org.junit.Assert; import org.junit.Test;
為什么需要兩個(gè)jar包呢,因?yàn)槲覀冊(cè)跍y(cè)試環(huán)境嘛
使用步驟
1.如果兩個(gè)值相等代碼演示
代碼如下(示例):
String s="Hello Maven"; ? Assert.assertEquals("Hello Maven",s);
正確情況下,不會(huì)有顯示,會(huì)正常執(zhí)行流程
如果錯(cuò)誤
public class Demo2 { ? ? @Test ? ? public void Demotest(){ ? ? ? ? //使用斷言 ? ? ? ? String s="Hello Maven"; ? ? ? ? Assert.assertEquals("Hell Maven",s); ? ? } }
在這里,我修改了值會(huì)有如下結(jié)果在控制臺(tái)顯示,不用看內(nèi)容啊
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
小結(jié)一下:對(duì)比可以是兩個(gè)數(shù)據(jù)的值,也可以是兩個(gè)數(shù)據(jù)的地址值,上面我們也演示了,當(dāng)兩個(gè)值正確情況下,測(cè)試模式可以正常運(yùn)行,如果錯(cuò)誤,會(huì)拋出異常,結(jié)束掉測(cè)試.
Assert.assertEquals是什么含義
assertEquals
函數(shù)原型1:
assertEquals([String message],expected,actual)?
參數(shù)說(shuō)明:
message
是個(gè)可選的消息,假如提供,將會(huì)在發(fā)生錯(cuò)誤時(shí)報(bào)告這個(gè)消息。expected
是期望值,通常都是用戶指定的內(nèi)容。actual
是被測(cè)試的代碼返回的實(shí)際值。
函數(shù)原型2:
assertEquals([String message],expected,actual,tolerance)?
參數(shù)說(shuō)明:
message
是個(gè)可選的消息,假如提供,將會(huì)在發(fā)生錯(cuò)誤時(shí)報(bào)告這個(gè)消息。expected
是期望值,通常都是用戶指定的內(nèi)容。actual
是被測(cè)試的代碼返回的實(shí)際值。tolerance
是誤差參數(shù),參加比較的兩個(gè)浮點(diǎn)數(shù)在這個(gè)誤差之內(nèi)則會(huì)被認(rèn)為是 相等的。
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
探索jedis連接池預(yù)熱優(yōu)化高并發(fā)
這篇文章主要為大家介紹了jedis連接池預(yù)熱優(yōu)化高并發(fā)深入探索示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-10-10教你怎么用java實(shí)現(xiàn)客戶端與服務(wù)器一問(wèn)一答
這篇文章主要介紹了教你怎么用java實(shí)現(xiàn)客戶端與服務(wù)器一問(wèn)一答,文中有非常詳細(xì)的代碼示例,對(duì)正在學(xué)習(xí)java的小伙伴們有非常好的幫助,需要的朋友可以參考下2021-04-04java 中List按照date排序的實(shí)現(xiàn)
這篇文章主要介紹了java 中List按照date排序的實(shí)現(xiàn)的相關(guān)資料,需要的朋友可以參考下2017-06-06jvm內(nèi)存溢出解決方法(jvm內(nèi)存溢出怎么解決)
jvm內(nèi)存溢出解決方法,詳細(xì)內(nèi)容看下面解釋2013-12-12SprinBoot如何集成參數(shù)校驗(yàn)Validator及參數(shù)校驗(yàn)的高階技巧
這篇文章主要介紹了SprinBoot如何集成參數(shù)校驗(yàn)Validator及參數(shù)校驗(yàn)的高階技巧包括自定義校驗(yàn)、分組校驗(yàn),本文分步驟給大家介紹的非常詳細(xì),需要的朋友可以參考下2022-05-05spring cloud實(shí)現(xiàn)Eureka注冊(cè)中心的HA的方法
本篇文章主要介紹了spring cloud實(shí)現(xiàn)Eureka注冊(cè)中心的HA的方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-01-01