Skip to content

Commit d29e3eb

Browse files
dmlloydmarcphilipp
authored andcommitted
Fix NPE when deserializing TestIdentifier (#3820)
An NPE was thrown when (de)serializing `TestIdentifiers` without a parent. Fixes #3819. Co-authored-by: Marc Philipp <mail@marcphilipp.de> (cherry picked from commit bc55d0e)
1 parent f936c01 commit d29e3eb

File tree

3 files changed

+22
-2
lines changed

3 files changed

+22
-2
lines changed

documentation/src/docs/asciidoc/release-notes/release-notes-5.10.3.adoc

+3
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ on GitHub.
1919
`@ConfigurationParameter` when selecting tests by `UniqueId`.
2020
* In order to support using `@EnabledInNativeImage` on test classes,
2121
`UniqueIdTrackingListener` now tracks descendants of skipped test containers.
22+
* Attempting to deserialize a `TestIdentifier` no longer causes a `NullPointerException`
23+
when there is no parent identifier. See
24+
link:https://github.com/junit-team/junit5/issues/3819[issue 3819].
2225

2326
==== Deprecations and Breaking Changes
2427

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

+4-2
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

+15
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)