Assert.assertEquals的使用方法及注意事項(xiàng)說明
Assert.assertEquals使用及注意事項(xiàng)
在開發(fā)中,我們需要測(cè)試時(shí)候,不可能把全部程序運(yùn)行一次,在此我們就需要通過編寫單元測(cè)試來對(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ù)說明:
message是個(gè)可選的消息,假如提供,將會(huì)在發(fā)生錯(cuò)誤時(shí)報(bào)告這個(gè)消息。expected是期望值,通常都是用戶指定的內(nèi)容。actual是被測(cè)試的代碼返回的實(shí)際值。
函數(shù)原型2:
assertEquals([String message],expected,actual,tolerance)?
參數(shù)說明:
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)文章
Java學(xué)習(xí)之反射機(jī)制及應(yīng)用場(chǎng)景介紹
本篇文章主要介紹了Java反射機(jī)制及應(yīng)用場(chǎng)景,反射機(jī)制是很多Java框架的基石。非常具有實(shí)用價(jià)值,需要的朋友可以參考下。2016-11-11
Netty分布式NioSocketChannel注冊(cè)到selector方法解析
這篇文章主要為大家介紹了Netty分布式源碼分析NioSocketChannel注冊(cè)到selector方法的解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-03-03
Spring注解@Configuration與@Bean注冊(cè)組件的使用詳解
這篇文章主要介紹了SpringBoot中的注解@Configuration與@Bean注冊(cè)組件的使用,具有很好的參考價(jià)值,希望對(duì)大家有所幫助2022-06-06
java?poi導(dǎo)入純數(shù)字等格式問題及解決
這篇文章主要介紹了java?poi導(dǎo)入純數(shù)字等格式問題及解決,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-03-03
java鏈?zhǔn)絼?chuàng)建json對(duì)象的實(shí)現(xiàn)
本文主要介紹了java中如何通過最簡(jiǎn)單的方式實(shí)現(xiàn)鏈?zhǔn)絼?chuàng)建json對(duì)象,解決創(chuàng)建json代碼臃腫的問題,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-02-02
Java 超詳細(xì)講解數(shù)據(jù)結(jié)構(gòu)的應(yīng)用
數(shù)據(jù)結(jié)構(gòu)是計(jì)算機(jī)存儲(chǔ)、組織數(shù)據(jù)的方式。數(shù)據(jù)結(jié)構(gòu)是指相互之間存在一種或多種特定關(guān)系的數(shù)據(jù)元素的集合,讓我們一起來了解數(shù)據(jù)結(jié)構(gòu)是如何應(yīng)用的2022-04-04
java實(shí)現(xiàn)json字符串格式化處理的工具類
這篇文章主要為大家詳細(xì)介紹了如何使用java實(shí)現(xiàn)json字符串格式化處理的工具類,文中的示例代碼簡(jiǎn)潔易懂,感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2024-01-01
Java中語(yǔ)音url轉(zhuǎn)換成InputStream的示例代碼
在Java中,可以使用java.net.URL和java.net.URLConnection類來將語(yǔ)音URL轉(zhuǎn)換為InputStream,本文通過示例代碼介紹Java中語(yǔ)音url轉(zhuǎn)換成InputStream的相關(guān)知識(shí),感興趣的朋友一起看看吧2024-01-01
Spring+Quartz配置定時(shí)任務(wù)實(shí)現(xiàn)代碼
這篇文章主要介紹了Spring+Quartz配置定時(shí)任務(wù)實(shí)現(xiàn)代碼,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-04-04

