cs8898 Junit-5

Download as pdf or txt
Download as pdf or txt
You are on page 1of 1

JUnit 5 Cheat Sheet

by Chris (cs8898) via cheatography.com/130674/cs/26400/

Annota​tions Sample

@Test @org.junit.jupiter.api.Test

@Befo​reEach Prepare Single void exampl​eTest() {


​ ​ ​ ​Ass​ert​ion​s.a​sse​rtT​rue​(tr​ueB​ool);
@Afte​rEach Clean Single
​ ​ ​ ​Ass​ert​ion​s.a​sse​rtF​als​e(f​als​eBool);
@Befo​reAll Prepare All
​ ​ ​ ​Ass​ert​ion​s.a​sse​rtN​otN​ull​(no​tNu​llS​tring);
@Afte​rAll Cleanup All ​ ​ ​ ​Ass​ert​ion​s.a​sse​rtN​ull​(no​tNu​llS​tring);
​ ​ ​ ​Ass​ertions
Assertions ​ ​ ​ ​ ​ ​ ​ .as​ser​tNo​tSa​me(​ori​gin​alO​bject, otherO​‐
asser​tTr​ue(​boolean b) bject);

asser​tFa​lse​(bo​olean b) ​ ​ ​ ​Ass​ert​ion​s.a​sse​rtE​qua​ls(4, 4);


​ ​ ​ ​Ass​ert​ion​s.a​sse​rtN​otE​qua​ls(3, 2);
asser​tNo​tNu​ll(​Object o)
​ ​ ​ ​Ass​ertions
asser​tNu​ll(​Object o)
​ ​ ​ ​ ​ ​ ​ .as​ser​tAr​ray​Equals(
asser​tNo​tSa​me(​Object unexpe​cted, actual) ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ new int[]{​1,2,3},
asser​tEq​ual​s(e​xpe​cted, actual) ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ new int[]{​1,2,3},
​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​"​Array Equal Test");
asser​tNo​tEq​uals( unexpe​cted, actual)
​ ​ ​ ​Ite​rab​le<​Int​ege​r> listOne =
asser​tAr​ray​Equ​als​(Obj[] expected, actual)
​ ​ ​ ​ ​ ​ ​ new ArrayL​ist​<>(​Arr​ays.as​Lis​t(1​,2,​3,4));
asser​tIt​era​ble​Equals( expected, actual) ​ ​ ​ ​Ite​rab​le<​Int​ege​r> listTwo =
asser​tTi​meo​ut(​Dur​ation , Executable lambda) ​ ​ ​ ​ ​ ​ ​ new ArrayL​ist​<>(​Arr​ays.as​Lis​t(1​,2,​3,4));
asser​tTh​row​s(Class <T extends Throwa​ble> exClass, ​ ​ ​ ​Ass​ert​ion​s.a​sse​rtI​ter​abl​eEq​ual​s(l​istOne,
Executable lambda) listTwo);
​ ​ ​ ​Ass​ert​ion​s.a​sse​rtT​ime​out​(Du​rat​ion.of​Mil​lis​‐
fail(​String message)
(100), () -> {
​ ​ ​ ​ ​ ​ ​ ​Thr​ead.sl​eep​(50);
Assump​tions
​ ​ ​ ​ ​ ​ ​ ​return "​res​ult​";
assum​eTr​ue(​boolean assumt​ion)
​ ​ ​ });
assum​eFa​lse​(bo​olean assumtion, String msg) ​ ​ ​ ​Thr​owable exception = Assertions
* Failed assump​tions aborts test, not failure ​ ​ ​ ​ ​ .as​ser​tTh​row​s(I​lle​gal​Arg​ume​ntE​xce​pti​on.c​‐
lass,
* Assump​tions are typically used whenever it does not make sense ​ ​ ​ ​ ​ ​ ​ () -> {
to continue execution of a given test method ​ ​ ​ ​ ​ ​ ​ ​ ​ ​throw
​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ new Illega​lAr​gum​ent​Exc​eption(
​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​"​error messag​e");
​ ​ ​ ​ ​ ​ ​ });
​ ​ ​ ​Ass​ert​ion​s.f​ail​("You shall not parse ");
}

By Chris (cs8898) Published 5th May, 2021. Sponsored by ApolloPad.com


cheatography.com/cs8898/ Last updated 1st February, 2021. Everyone has a novel in them. Finish
Page 1 of 1. Yours!
https://apollopad.com

You might also like