Skip to content

Commit 664470c

Browse files
testfixertuteng
authored andcommitted
Make tests more stable by using JSONAssert equals (#6247)
`avroSchema.getSchemaInfo().getSchema()` in `src/test/java/org/apache/pulsar/client/impl/schema/AvroSchemaTest.java` returns a JSON object. `schemaJson` compares with hard-coded JSON String. However, the order of entries in `schemaJson` is not guaranteed so the json results can contain entries in any order. This PR proposes to use JSONAssert and modify the corresponding json test assertions so that the test is more stable. ### Motivation Using JSONAssert and modifying the corresponding json test assertions so that the test is more stable. ### Modifications Adding `assertJSONEquals` method and replacing `assertEquals` with it in tests `testNotAllowNullSchema` and `testAllowNullSchema`. (cherry picked from commit 7dd52b8)
1 parent 731211c commit 664470c

File tree

3 files changed

+18
-4
lines changed

3 files changed

+18
-4
lines changed

pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,7 @@ flexible messaging model and an intuitive client API.</description>
215215
<powermock.version>2.0.2</powermock.version>
216216
<javassist.version>3.25.0-GA</javassist.version>
217217
<failsafe.version>2.3.1</failsafe.version>
218+
<skyscreamer.version>1.5.0</skyscreamer.version>
218219

219220
<!-- Plugin dependencies -->
220221
<protobuf-maven-plugin.version>0.6.1</protobuf-maven-plugin.version>

pulsar-client/pom.xml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,13 @@
151151
<version>${project.parent.version}</version>
152152
<scope>test</scope>
153153
</dependency>
154+
155+
<dependency>
156+
<groupId>org.skyscreamer</groupId>
157+
<artifactId>jsonassert</artifactId>
158+
<version>${skyscreamer.version}</version>
159+
<scope>test</scope>
160+
</dependency>
154161
</dependencies>
155162

156163
<build>

pulsar-client/src/test/java/org/apache/pulsar/client/impl/schema/AvroSchemaTest.java

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@
5353
import org.apache.pulsar.client.impl.schema.SchemaTestUtils.Foo;
5454
import org.apache.pulsar.common.schema.SchemaInfo;
5555
import org.apache.pulsar.common.schema.SchemaType;
56+
import org.json.JSONException;
57+
import org.skyscreamer.jsonassert.JSONAssert;
5658
import org.testng.Assert;
5759
import org.testng.annotations.Test;
5860

@@ -142,13 +144,17 @@ public void testSchemaDefinition() throws SchemaValidationException {
142144
}
143145
}
144146

147+
public void assertJSONEquals(String s1, String s2) throws JSONException {
148+
JSONAssert.assertEquals(s1, s2, false);
149+
}
150+
145151
@Test
146-
public void testNotAllowNullSchema() {
152+
public void testNotAllowNullSchema() throws JSONException {
147153
AvroSchema<Foo> avroSchema = AvroSchema.of(SchemaDefinition.<Foo>builder().withPojo(Foo.class).withAlwaysAllowNull(false).build());
148154
assertEquals(avroSchema.getSchemaInfo().getType(), SchemaType.AVRO);
149155
Schema.Parser parser = new Schema.Parser();
150156
String schemaJson = new String(avroSchema.getSchemaInfo().getSchema());
151-
assertEquals(schemaJson, SCHEMA_AVRO_NOT_ALLOW_NULL);
157+
assertJSONEquals(schemaJson, SCHEMA_AVRO_NOT_ALLOW_NULL);
152158
Schema schema = parser.parse(schemaJson);
153159

154160
for (String fieldName : FOO_FIELDS) {
@@ -165,13 +171,13 @@ public void testNotAllowNullSchema() {
165171
}
166172

167173
@Test
168-
public void testAllowNullSchema() {
174+
public void testAllowNullSchema() throws JSONException {
169175
AvroSchema<Foo> avroSchema = AvroSchema.of(SchemaDefinition.<Foo>builder().withPojo(Foo.class).build());
170176
assertEquals(avroSchema.getSchemaInfo().getType(), SchemaType.AVRO);
171177
Schema.Parser parser = new Schema.Parser();
172178
parser.setValidateDefaults(false);
173179
String schemaJson = new String(avroSchema.getSchemaInfo().getSchema());
174-
assertEquals(schemaJson, SCHEMA_AVRO_ALLOW_NULL);
180+
assertJSONEquals(schemaJson, SCHEMA_AVRO_ALLOW_NULL);
175181
Schema schema = parser.parse(schemaJson);
176182

177183
for (String fieldName : FOO_FIELDS) {

0 commit comments

Comments
 (0)