0

I have the following function:

public class NorthboundApiLogicTest {

  @Test
  public void testIfParametersValuesAreCorrect() {
    String body = "{\"exception\":\"world\",\"error\":\"Not Found\",\"message\":\"hello\""
        + ",\"timestamp\":\"2020-01-07T12:26:48.334386Z\",\"status\":\"404\"}";
    try {
      JSONAssert.assertEquals("{message:hello, exception:world, status:403, error:Not Found}", 
          body, false);
    } catch (Exception e) {
      System.err.println(e);
    }
    System.out.println(body);
  }
}

I run this test with Maven, and strangely it pass successfully.

enter image description here

However, it should not, because I assert that status=403 but the value is 404. What am I doing wrong?

1
  • 2
    Note that JSONException thrown and you're in catch. Commented Jan 7, 2020 at 12:42

1 Answer 1

4

It's failing to parse the JSON before it even performs an assertion on the structure, and you're simply logging that via your catch.

catch (Exception e) {
      System.err.println(e);
}

You need to let that exception be thrown from the method, or catch it and call fail() with an appropriate message.

And then fix the issue of your non-parseable JSON, of course!

Sign up to request clarification or add additional context in comments.

2 Comments

Strange because according to this jsonlint.com my JSON is valid.
{message:hello, exception:world, status:403, error:Not Found} is not valid json

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.