2

I want to pass my test.When I run my test it gets fail because expected and actual is not matching. How to do with assert command?

When I copy the actual value it shows like an error:

{"level":"error","msg":"REG_DEV_EXISTS_00","meta":[]}"}

Here is my code:

public class registerCounter {
    @Test(dataProvider = "register")
    public void md5JsonTest(String secret,String mac,String id, String md5Hash) {

        given().
                parameters("s", secret).parameters("m",mac).parameters("d",id).
                when().
                get("http://baseUrl/register/device").
                then().
                assertThat().
                body("md5", equalTo(md5Hash));
    }

    @DataProvider(name = "register")
    public Object[][] createMD5TestData() {

        return new String[][] {

                {"b30b33","d3r34ret34t5r","8","REG_DEV_EXISTS_00"}

        };
    }

}

Error:

java.lang.AssertionError: XML path md5 doesn't match.  
Expected:  REG_DEV_EXISTS_00    
Actual: {"level":"error","msg":"REG_DEV_EXISTS_00","meta":[]}

I put containsString

java.lang.AssertionError: XML path md5 doesn't match. 
Expected: a string containing "REG_DEV_EXISTS_00" 
Actual: {"level":"error","msg":"REG_DEV_EXISTS_00","meta":[]} 
1

Assert :

  • Reminder : If Assert get failed the execution or program get terminated there itself.

  • It can be used as follows:

    Assert.assertEquals(driver.findElement(By.id("your_element_id")).getText(), "Expected Text");
    
                             OR
    
    Assert.assertEquals(driver.findElement(By.id("your_element_id")).getText(), "Expected Text","Assert failed - text does not match");
    
  • hai i am using rest-assured. can you check my code and say correct assert command? – user21268 Dec 15 '16 at 6:26
0

You could do an Assert that the actual value containsString of the expected value.

assertThat().
body("md5", containsString(md5Hash));
  • java.lang.AssertionError: XML path md5 doesn't match. Expected: a string containing "REG_DEV_EXISTS_00" Actual: {"level":"error","msg":"REG_DEV_EXISTS_00","meta":[]} I got this error what to do to pass my test ? – user21268 Dec 20 '16 at 16:28
0

You're comparing your expected value (REG_DEV_EXISTS_00) with a piece of JSON that contains the expected value and some other data

({"level":"error","msg":"REG_DEV_EXISTS_00","meta":[]}) 

Either use the containsString assert as kirbycope suggested, or parse the JSON and extract the relevant msg field before doing the comparison.

  • java.lang.AssertionError: XML path md5 doesn't match. Expected: a string containing "REG_DEV_EXISTS_00" Actual: {"level":"error","msg":"REG_DEV_EXISTS_00","meta":[]} – user21268 Dec 20 '16 at 16:29