-
Notifications
You must be signed in to change notification settings - Fork 54.3k
BAEL-9317 Parsing JSON boolean Value in Java - improve to talk about 0/1 representation #18573
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've left a few comments for you to consider.
json-modules/json/src/test/java/com/baeldung/jsonjava/ParseJsonBooleanUnitTest.java
Outdated
Show resolved
Hide resolved
|
||
@Test | ||
void givenJSONString_whenParsed_correctBooleanValueReturned() { | ||
String jsonString = "{\"name\":\"lorem ipsum\",\"active\":true,\"id\":1}"; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please use text blocks for JSON.
Also, consider defining a member variable template since most of these tests have the same construct:
private final String json = """
{
"name": "lorem ipsum",
"active": %s,
"id": %d
}""";
void givenJSONWithBooleanAs0Or1_whenParsed_correctBooleanValueReturned() { | ||
String jsonString = "{\"name\":\"lorem ipsum\",\"active\":1,\"id\":1}"; | ||
JSONObject jsonObject = new JSONObject(jsonString); | ||
int activeInt = jsonObject.getInt("active"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
From the guidelines:
when creating complex assertions, it’s a good idea to use assertj to preserve clarity - if assertions are trivial, it’s fine to stick to JUnit ones
If you do this, then the test becomes:
assertThat(jsonObject.getInt("active").isEqualTo(1);
which is quite succinct.
assertEquals(type,"Feature"); | ||
assertEquals(geometry,"Point"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please reverse the order of the arguments. It's intended to be assertEquals(expected, actual)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Reversed
No description provided.