Vous êtes sur la page 1sur 4

10/6/2016

MuleSoft:FunctionalTestCaseDZonePerformance

MuleSoft:FunctionalTestCase
bySulthonyHMay.30,16PerformanceZone

DownloadForrestersVendorLandscape,ApplicationPerformanceManagementreport
thatexaminestheevolvingroleofAPMasakeydriverofcustomersatisfactionand
businesssuccess,broughttoyouinpartnershipwithBMC.
OncewehaveutilizedaJavaComponentinAnypointStudio,thenextchallengeis:
Howdowetestit?
Fortestingpurposes,wecanuseMUnit.MUnitisaMuleapplicationtestingframeworkwhichallows
ustoeasilybuildautomatedtests.WithMUnit,wecancreateMuletestsbywritingcommonJavacode,
slightlysimilartoJunit.Naturally,itismorecomfortableforJavadevelopers.
Topracticethis,wecanuseanyJavaclassinanyAnypointproject.However,itissuggestedtofollow
theexampleinthepreviouspost:MuleSoft:MethodEntryPointResolver,astheJavacodeexamplein
thispostreferstoit.
1.OpenPackageExplorer,thenrightclickonsrc/test/java.Choose:New>>Class.
2.SetthePackageas:com.mulesoft.training
3.SettheNameas:MathOperationTest
4.SettheSuperclassas:org.mule.tck.junit4.FunctionalTestCase
Note:wecantypeitdirectlyorclicktheBrowsebutton

5.ClicktheFinishbutton
AnewemptyJavaclasswillbecreatedandopened:
1

packagecom.mulesoft.training;

2
3

importorg.mule.tck.junit4.FunctionalTestCase;

4
5

publicclassMathOperationTestextendsFunctionalTestCase{

https://dzone.com/articles/mulesoftfunctionaltestcase

1/4

10/6/2016

MuleSoft:FunctionalTestCaseDZonePerformance

6
7

OurTestclassmustextendFunctionalTestCase,abasetestcaseforMulefunctionaltests.Itcontainsa
numberofsupportingclassesandcontexts,whichwillmakeiteasytodothetesting.
BeforecompletingourTestclass,thefirstthingthatwehavetodoisspecifytheconfigurationfiletobe
tested.Todothat,wehavetooverridethegetConfigResources()methodandthenspecifythefile
name.
1

packagecom.mulesoft.training;

2
3

importorg.mule.tck.junit4.FunctionalTestCase;

4
5

publicclassMathOperationTestextendsFunctionalTestCase{

protectedStringgetConfigResources(){

return"mathoperation.xml";

Note:wecanspecifymorethanonefileusingacommaseparatedlist.
NowwewillcompleteoursimplemathematicoperationbytestingtheMathOperationclassdirectly
throughthemathoperationFlow.
TotesttheJavaclassdirectly,itisnotdifferentfromacommonJUnitoperation.Pleaserefertothe
examplebelow,andrunaJUnittestbychoosing:RunAs>>JUnitTest
1

packagecom.mulesoft.training;

2
3

importstaticorg.junit.Assert.assertEquals;

importorg.junit.Test;

importorg.mule.tck.junit4.FunctionalTestCase;

6
7

publicclassMathOperationTestextendsFunctionalTestCase{

protectedStringgetConfigResources(){

return"mathoperation.xml";

10

11
12

@Test

13

publicvoidtestJavaClass(){

14

Stringvalue="4";

15

MathOperationmathOperation=newMathOperation();

16

17

assertEquals("16.0",mathOperation.power(value));

18

assertEquals("2.0",mathOperation.squareRoot(value));

https://dzone.com/articles/mulesoftfunctionaltestcase

2/4

10/6/2016

MuleSoft:FunctionalTestCaseDZonePerformance

18

assertEquals("2.0",mathOperation.squareRoot(value));

19

20

Bychoosingthisapproachwecantestthespecificfunction.Ifrequired,wecantesttheflowtomake
sureitsbehaviorisappropriate.Forthispurpose,addthissnippetintothecurrentTestclass:
1

@Test

publicvoidtestFlow()throwsException{

MuleEventevent=runFlow("mathoperationFlow","4");

MuleMessagemessage=event.getMessage();

5
6

assertNotNull(message);

assertNotNull(message.getPayload());

assertEquals("16.0",message.getPayload());

Here,weutilizerunFlowtocalltheflowbyitsname,andsendthepayloadasasecondargument.The
behavioroftherestofthecodeissimilarwhenwerunMuleondebugmode.Wecanevaluatethe
message,payload,etc.
Anotherapproachtotestourintegrationprojectistestingtheinboundendpoint.Itsimulatesthereal
executionoftheflowbyusingMuleClienttointeractwiththerunningMuleserver.Basically,thereare
manyoptionsforthisinteraction.Inthisexample,wespecifytheURLasthefirstargument,the
payloadasthesecondargument,andsetnullformessageproperties(itisnotneededinthisexample)
asthethirdargument.
1

@Test

publicvoidtestInboundEndpoint()throwsException{

MuleClientclient=newMuleClient(muleContext);

MuleMessagemessage=client.send("http://localhost:8081/power","4",null);

5
6

assertNotNull(message);

assertNotNull(message.getPayload());

assertEquals("16.0",message.getPayloadAsString());

ThatishowtotestaJavaComponentinAnypointstudio.Sofeelfreetotrythedifferentoptionsand
choosewhichonesuitsyourintegrationproject.
Happytesting!

SeeForrestersReport,VendorLandscape,ApplicationPerformanceManagementto
identifytherightvendortohelpITdeliverbetterserviceatalowercost,broughttoyouin
partnershipwithBMC.

ThebestofDZonestraighttoyourinbox.

SEEANEXAMPLE
https://dzone.com/articles/mulesoftfunctionaltestcase

3/4

10/6/2016

Topics:MULESOFT

MuleSoft:FunctionalTestCaseDZonePerformance

SEEANEXAMPLE

SUBSCRIBE

ThebestofDZonestraighttoyourinbox.
SEEANEXAMPLE
https://dzone.com/articles/mulesoftfunctionaltestcase

4/4

Vous aimerez peut-être aussi