Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
12 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,6 +14,7 @@ | |
|
||
import java.lang.reflect.Method; | ||
import java.util.List; | ||
import java.util.Optional; | ||
|
||
import org.junit.gen5.commons.util.ReflectionUtils; | ||
import org.junit.gen5.engine.AbstractTestDescriptor; | ||
|
@@ -144,7 +145,11 @@ private void resolveContextTestable(String uniqueId, Class<?> testClass, Class<? | |
|
||
private TestDescriptor resolveAndReturnParentTestable(JUnit5Testable containerTestable) { | ||
resolveTestable(containerTestable, false); | ||
return descriptorByUniqueId(containerTestable.getUniqueId()); | ||
return descriptorByUniqueId(containerTestable.getUniqueId()).orElseThrow(() -> { | ||
String errorMessage = String.format("Testable with unique id %s could not be resolved. Programming error!", | ||
containerTestable.getUniqueId()); | ||
return new RuntimeException(errorMessage); | ||
}); | ||
} | ||
|
||
private void resolveContainedTestMethods(Class<?> testClass, AbstractTestDescriptor parentDescriptor) { | ||
|
@@ -167,32 +172,20 @@ private void resolveContainedContexts(Class<?> clazz) { | |
} | ||
|
||
private MethodTestDescriptor getOrCreateMethodDescriptor(Method method, String uniqueId) { | ||
MethodTestDescriptor methodTestDescriptor = (MethodTestDescriptor) descriptorByUniqueId(uniqueId); | ||
if (methodTestDescriptor == null) { | ||
methodTestDescriptor = new MethodTestDescriptor(uniqueId, method); | ||
} | ||
return methodTestDescriptor; | ||
return (MethodTestDescriptor) descriptorByUniqueId(uniqueId).orElse(new MethodTestDescriptor(uniqueId, method)); | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
jlink
via email
Author
Contributor
|
||
} | ||
|
||
private ContextTestDescriptor getOrCreateContextDescriptor(Class<?> clazz, String uniqueId) { | ||
ContextTestDescriptor contextTestDescriptor = (ContextTestDescriptor) descriptorByUniqueId(uniqueId); | ||
if (contextTestDescriptor == null) { | ||
contextTestDescriptor = new ContextTestDescriptor(uniqueId, clazz); | ||
} | ||
return contextTestDescriptor; | ||
return (ContextTestDescriptor) descriptorByUniqueId(uniqueId).orElse( | ||
new ContextTestDescriptor(uniqueId, clazz)); | ||
} | ||
|
||
private ClassTestDescriptor getOrCreateClassDescriptor(Class<?> clazz, String uniqueId) { | ||
ClassTestDescriptor classTestDescriptor = (ClassTestDescriptor) descriptorByUniqueId(uniqueId); | ||
if (classTestDescriptor == null) { | ||
classTestDescriptor = new ClassTestDescriptor(uniqueId, clazz); | ||
} | ||
return classTestDescriptor; | ||
return (ClassTestDescriptor) descriptorByUniqueId(uniqueId).orElse(new ClassTestDescriptor(uniqueId, clazz)); | ||
} | ||
|
||
private TestDescriptor descriptorByUniqueId(String uniqueId) { | ||
// TODO Users of this method should use Optional directly but I couldn't figure out how. | ||
return engineDescriptor.findByUniqueId(uniqueId).orElse(null); | ||
private Optional<TestDescriptor> descriptorByUniqueId(String uniqueId) { | ||
return engineDescriptor.findByUniqueId(uniqueId); | ||
} | ||
|
||
} |
I think it would make sense to use
Optional.orElseGet(Supplier)
in these cases so the fallback object only gets instantiated when needed.