Skip to content

Commit bc55d0e

Browse files
dmlloydmarcphilipp
andauthored
Fix NPE when deserializing TestIdentifier (#3820)
An NPE was thrown when (de)serializing `TestIdentifiers` without a parent. Fixes #3819. Co-authored-by: Marc Philipp <[email protected]>
1 parent f18e745 commit bc55d0e

File tree

3 files changed

+20
-2
lines changed

3 files changed

+20
-2
lines changed

documentation/src/docs/asciidoc/release-notes/release-notes-5.11.0-M2.adoc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ repository on GitHub.
2626
[[release-notes-5.11.0-M2-junit-platform-bug-fixes]]
2727
==== Bug Fixes
2828

29+
* Fixed a bug where `TestIdentifier` could cause a `NullPointerException` on deserialize when there is no parent identifier. See link:https://github.com/junit-team/junit5/issues/3819[issue 3819].
2930
* ❓
3031

3132
[[release-notes-5.11.0-M2-junit-platform-deprecations-and-breaking-changes]]

junit-platform-launcher/src/main/java/org/junit/platform/launcher/TestIdentifier.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,8 @@ private void readObject(ObjectInputStream s) throws ClassNotFoundException, IOEx
284284
source = serializedForm.source;
285285
tags = serializedForm.tags;
286286
type = serializedForm.type;
287-
parentId = UniqueId.parse(serializedForm.parentId);
287+
String parentId = serializedForm.parentId;
288+
this.parentId = parentId == null ? null : UniqueId.parse(parentId);
288289
legacyReportingName = serializedForm.legacyReportingName;
289290
}
290291

@@ -307,7 +308,8 @@ private static class SerializedForm implements Serializable {
307308

308309
SerializedForm(TestIdentifier testIdentifier) {
309310
this.uniqueId = testIdentifier.uniqueId.toString();
310-
this.parentId = testIdentifier.parentId.toString();
311+
UniqueId parentId = testIdentifier.parentId;
312+
this.parentId = parentId == null ? null : parentId.toString();
311313
this.displayName = testIdentifier.displayName;
312314
this.legacyReportingName = testIdentifier.legacyReportingName;
313315
this.source = testIdentifier.source;

platform-tests/src/test/java/org/junit/platform/launcher/TestIdentifierTests.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,21 @@ void initialVersionCanBeDeserialized() throws Exception {
7272
}
7373
}
7474

75+
@Test
76+
void identifierWithNoParentCanBeSerializedAndDeserialized() throws Exception {
77+
TestIdentifier originalIdentifier = TestIdentifier.from(
78+
new AbstractTestDescriptor(UniqueId.root("example", "id"), "Example") {
79+
@Override
80+
public Type getType() {
81+
return Type.CONTAINER;
82+
}
83+
});
84+
85+
var deserializedIdentifier = (TestIdentifier) deserialize(serialize(originalIdentifier));
86+
87+
assertDeepEquals(originalIdentifier, deserializedIdentifier);
88+
}
89+
7590
private static void assertDeepEquals(TestIdentifier first, TestIdentifier second) {
7691
assertEquals(first, second);
7792
assertEquals(first.getUniqueId(), second.getUniqueId());

0 commit comments

Comments
 (0)