Assertion error on using hamcrest library | Rest Assured Forum
C
chaitra jagirdar Posted on 25/02/2020

Am getting following assertion error:

FAILED: test005
java.lang.AssertionError: 1 expectation failed.
JSON path items[0].giftOptions doesn't match.
Expected: (a collection containing map containing ["allowGiftWrap"->"false"])
Actual: {allowGiftReceipt=false, allowGiftWrap=false, allowGiftMessage=false}

at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.base/java.lang.reflect.Constructor.newInstanceWithCaller(Constructor.java:500)
at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:481)
at org.codehaus.groovy.reflection.CachedConstructor.invoke(CachedConstructor.java:83)
at org.codehaus.groovy.reflection.CachedConstructor.doConstructorInvoke(CachedConstructor.java:77)
at org.codehaus.groovy.runtime.callsite.ConstructorSite$ConstructorSiteNoUnwrap.callConstructor(ConstructorSite.java:84)
at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCallConstructor(CallSiteArray.java:60)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callConstructor(AbstractCallSite.java:235)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callConstructor(AbstractCallSite.java:247)
at io.restassured.internal.ResponseSpecificationImpl$HamcrestAssertionClosure.validate(ResponseSpecificationImpl.groovy:482)
at io.restassured.internal.ResponseSpecificationImpl$HamcrestAssertionClosure$validate$1.call(Unknown Source)
at io.restassured.internal.ResponseSpecificationImpl.validateResponseIfRequired(ResponseSpecificationImpl.groovy:654)
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:567)
at org.codehaus.groovy.runtime.callsite.PogoMetaMethodSite$PogoCachedMethodSiteNoUnwrapNoCoerce.invoke(PogoMetaMethodSite.java:210)
at org.codehaus.groovy.runtime.callsite.PogoMetaMethodSite.callCurrent(PogoMetaMethodSite.java:59)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callCurrent(AbstractCallSite.java:166)
at io.restassured.internal.ResponseSpecificationImpl.content(ResponseSpecificationImpl.groovy:276)
at io.restassured.specification.ResponseSpecification$content$0.callCurrent(Unknown Source)
at io.restassured.internal.ResponseSpecificationImpl.body(ResponseSpecificationImpl.groovy:259)
at io.restassured.internal.ValidatableResponseOptionsImpl.body(ValidatableResponseOptionsImpl.java:274)
at apiary.Hamcrest.test005(Hamcrest.java:58)
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:567)
at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:124)
at org.testng.internal.Invoker.invokeMethod(Invoker.java:571)
at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:707)
at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:979)
at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:125)
at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:109)
at org.testng.TestRunner.privateRun(TestRunner.java:648)
at org.testng.TestRunner.run(TestRunner.java:505)
at org.testng.SuiteRunner.runTest(SuiteRunner.java:455)
at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:450)
at org.testng.SuiteRunner.privateRun(SuiteRunner.java:415)
at org.testng.SuiteRunner.run(SuiteRunner.java:364)
at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:84)
at org.testng.TestNG.runSuitesSequentially(TestNG.java:1187)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1116)
at org.testng.TestNG.runSuites(TestNG.java:1028)
at org.testng.TestNG.run(TestNG.java:996)
at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:115)
at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:251)
at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:77)

 

 

===============================================================

For test:

@Test
public void test005(){

given().get().then().body("items[0].giftOptions", hasItems(hasEntry("allowGiftWrap", "false")));


}

Have attached the snapshot of json in jsonviewer.Please suggest what's the problem here?


A
Ashish Thakur Replied on 28/02/2020

java.lang.AssertionError: 1 expectation failed.
JSON path items[0].giftOptions doesn't match.

giftOptions is not there


C
chaitra jagirdar Replied on 28/02/2020

{
"items": [
{
"itemId": 54518466,
"parentItemId": 54518466,
"name": "Acer CB3-532-C47C 15.6\" Chromebook, Chrome OS, Intel Celeron N3060 Dual-Core Processor, 2GB RAM, 16GB Internal Storage",
"msrp": 199,
"salePrice": 179,
"upc": "888863811427",
"categoryPath": "Electronics/Computers/Laptops/Shop Laptops by Type/All Laptop Computers",
"shortDescription": "Acer expands upon its already dominant Chromebook position with an affordable 15.6" HD Chromebook, letting you see more, do more and explore more than ever before. With a 27 percent larger screen area than the 13.3-inch Chromebook and a 24 percent larger screen than a 14-inch Chromebook, more content can be shown with less scrolling, and productivity tools such as Excel are easier to interact with. In short, it's just the right size for you to make this Chromebook your quot;everythingquot; book. The smart design of the Acer Chromebook brings videos and games to the next level with a stunning large-scale 15.6-inch display. The antiglare HD display is designed to minimize the glare resulting in less strain on the eyes, even after long hours of use. From bedtime storytelling to watching a movie, you'll experience a consistent color and viewing experience from any perspective.",
"longDescription": "<ul><li><br><b>Acer CB3-532-C47C 15.6" Chromebook:</b><br><br><b>Key Features and Benefits:</b><ul><li>15.6" display<br>LED-backlit Display<br><br></li><li>Intel Celeron N3060 Dual-Core processor<br>1.6GHz (with Max Turbo Speed of 2.48GHz)<br><br></li><li>2GB system memory<br>Gives you options for surfing, video conferencing, documents, basic photo editing and simple computer tasks<br><br></li><li>16GB internal storage<br>Store up to 10,667 photos, 4,500 songs or 8.4 hours of video and more<br><br></li><li>12 hours of battery life<br>Long-lasting battery life gives you all day to access your photos, videos, music and documents<br><br></li><li>Intel HD Graphics 400<br><br></li><li>Weight: 4.3 lbs<br>Thin design you can easily take on the road<br><br></li><li>802.11ac Wireless LAN<br>Wirelessly connect to a WiFi signal or hotspot with the 802.11ac connection built into your PC</li></ul><br><b>Additional Features:</b><ul><li>720 HD webcam with HRD (High Dynamic Range)</li><li>Bluetooth 4.2</li><li>Standard mini keyboard</li><li>2 x USB 3.0 ports, 1 x HDMI out port, 1 x 3.5mm audio out jack</li><li>3-cell lithium-ion battery</li><li>Dimensions: 15.08" x 10.08" x 0.95"</li><li>Color: Black</li></ul><br><b>Software:</b><ul><li>Chrome OS</li></ul><br><b>Support and Warranty:</b><ul><li>1-year limited hardware warranty</li><li>24/7 technical assistance available online or toll-free by phone</li></ul><br><b>What's In The Box:</b><ul><li>Power cord and AC adapter</li><li>3-cell battery</li><li>Quick Start Guide</li></ul><br><b><i>To see the manufacturer's specifications for this product, click here.<br><br>To see a list of our PC Accessories, click here.<br><br>Trade in your used computer and electronics for more cash to spend at Walmart. Good for your wallet and the environment - click here.</i></b><br><br><b>What is a Chromebook PC?</b><br>Chromebook PCs are a new type of computer designed to help you get things done faster and easier. Chromebook starts up in seconds, so you can immediately start playing or working. Chromebook looks like a laptop and feels like a laptop, but differs from traditional computers as follows:<ul><li>Instead of Windows OS or Apple OS, Chromebook PCs run on Chrome OS, an operating system that features multiple layers of security, cloud storage and the most popular Google products built right in.</li><li>Chromebook won't load and run traditional PC applications such as Microsoft Office or Skype. However, Chromebook uses Chrome-specific apps for everyday tasks. You'll be able to create documents, listen to music, edit your photos, chat with friends and family and play online games. Plus, you can download many additional apps from the Chrome Web Store. No more buying and installing software â</li><li>thousands of apps are free and feature automatic updates.</li><li>Chromebook comes with built-in virus protection, multiple layers of security and verified boot help to keep you safe from viruses and malware.</li><li>Your Chromebook will keep your files away from your machine and safe online. Google Drive is built right in, so your files and photos are automatically backed up to the cloud.</li><li>Your Chromebook will update itself for free, so you'll always have the latest and greatest version. No need for manual downloads and upgrades.</li></ul></li></ul>",
"brandName": "Acer",
"thumbnailImage": "https://i5.walmartimages.com/asr/f3077845-8786-4bfa-ba98-482f06af91a2_1.2bdade6ddc986cbd875304164a98aa06.jpeg?odnHeight=100&odnWidth=100&odnBg=FFFFFF",
"mediumImage": "https://i5.walmartimages.com/asr/f3077845-8786-4bfa-ba98-482f06af91a2_1.2bdade6ddc986cbd875304164a98aa06.jpeg?odnHeight=180&odnWidth=180&odnBg=FFFFFF",
"largeImage": "https://i5.walmartimages.com/asr/f3077845-8786-4bfa-ba98-482f06af91a2_1.2bdade6ddc986cbd875304164a98aa06.jpeg?odnHeight=450&odnWidth=450&odnBg=FFFFFF",
"productTrackingUrl": "http://goto.walmart.com/c/|PUBID|/568844/9383?veh=aff&sourceid=imp_000011112222333344&prodsku=54518466&u=https%3A%2F%2Fwww.walmart.com%2Fip%2F54518466",
"ninetySevenCentShipping": false,
"standardShipRate": 0,
"color": "GrayGranite Gray",
"shipToStore": true,
"freeShipToStore": true,
"modelNumber": "CB3-532-C47C",
"productUrl": "http://c.affil.walmart.com/t/api01?l=https%3A%2F%2Fwww.walmart.com%2Fip%2FAcer-CB3-532-C47C-15-6-Chromebook-Chrome-OS-Intel-Celeron-N3060-Dual-Core-Processor-2GB-RAM-16GB-Internal-Storage%2F54518466%3Faffp1%3D%7Capk%7C%26affilsrc%3Dapi%26veh%3Daff%26wmlspartner%3Dreadonlyapi",
"customerRating": "4.096",
"numReviews": 343,
"customerRatingImage": "http://i2.walmartimages.com/i/CustRating/4_1.gif",
"bestMarketplacePrice": {
"price": 349.98,
"sellerInfo": "TCH Apparel",
"standardShipRate": 0,
"availableOnline": true,
"clearance": false,
"offerType": "ONLINE_ONLY"
},
"categoryNode": "3944_3951_1089430",
"rhid": "32311",
"bundle": false,
"clearance": false,
"preOrder": false,
"stock": "Available",
"attributes": {
"batteryLife": "12 h",
"color": "Granite Gray",
"isOrderable": "Y"
},
"addToCartUrl": "http://c.affil.walmart.com/t/api01?l=http%3A%2F%2Faffil.walmart.com%2Fcart%2FaddToCart%3Fitems%3D54518466%7C1%26affp1%3D%7Capk%7C%26affilsrc%3Dapi%26veh%3Daff%26wmlspartner%3Dreadonlyapi",
"affiliateAddToCartUrl": "http://goto.walmart.com/c/|PUBID|/568844/9383?veh=aff&sourceid=imp_000011112222333344&prodsku=54518466&u=http%3A%2F%2Faffil.walmart.com%2Fcart%2FaddToCart%3Fitems%3D54518466",
"freeShippingOver35Dollars": true,
"giftOptions": {
"allowGiftWrap": false,
"allowGiftMessage": false,
"allowGiftReceipt": false
},
"imageEntities": [
{
"thumbnailImage": "https://i5.walmartimages.com/asr/ba4b3688-1bea-4002-a3e4-4078cf3c8901_1.7775b898501ddfa1566ae67079d57af8.jpeg?odnHeight=100&odnWidth=100&odnBg=FFFFFF",
"mediumImage": "https://i5.walmartimages.com/asr/ba4b3688-1bea-4002-a3e4-4078cf3c8901_1.7775b898501ddfa1566ae67079d57af8.jpeg?odnHeight=180&odnWidth=180&odnBg=FFFFFF",
"largeImage": "https://i5.walmartimages.com/asr/ba4b3688-1bea-4002-a3e4-4078cf3c8901_1.7775b898501ddfa1566ae67079d57af8.jpeg?odnHeight=450&odnWidth=450&odnBg=FFFFFF",
"entityType": "SECONDARY"
},
{
"thumbnailImage": "https://i5.walmartimages.com/asr/f3077845-8786-4bfa-ba98-482f06af91a2_1.2bdade6ddc986cbd875304164a98aa06.jpeg?odnHeight=100&odnWidth=100&odnBg=FFFFFF",
"mediumImage": "https://i5.walmartimages.com/asr/f3077845-8786-4bfa-ba98-482f06af91a2_1.2bdade6ddc986cbd875304164a98aa06.jpeg?odnHeight=180&odnWidth=180&odnBg=FFFFFF",
"largeImage": "https://i5.walmartimages.com/asr/f3077845-8786-4bfa-ba98-482f06af91a2_1.2bdade6ddc986cbd875304164a98aa06.jpeg?odnHeight=450&odnWidth=450&odnBg=FFFFFF",
"entityType": "PRIMARY"
}
],
"offerType": "ONLINE_AND_STORE",
"isTwoDayShippingEligible": true,
"availableOnline": true
}
]
}


C
chaitra jagirdar Replied on 28/02/2020

"giftOptions": {
"allowGiftWrap": false,
"allowGiftMessage": false,
"allowGiftReceipt": false
}

 

Does exist in json.....Please have a look in the json viewer :items[0].giftOptions