Skip to content

Commit

Permalink
Extract instantiateComponents() method in AbstractTestContextBootstra…
Browse files Browse the repository at this point in the history
…pper
  • Loading branch information
sbrannen committed Sep 14, 2023
1 parent 5996196 commit 09b1e5e
Showing 1 changed file with 15 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,11 @@ else if (logger.isDebugEnabled()) {
return listeners;
}

@SuppressWarnings("unchecked")
private List<TestExecutionListener> instantiateListeners(Class<? extends TestExecutionListener>... classes) {
return instantiateComponents(TestExecutionListener.class, classes);
}

/**
* Get the default {@link TestExecutionListener TestExecutionListeners} for
* this bootstrapper.
Expand All @@ -199,33 +204,6 @@ protected List<TestExecutionListener> getDefaultTestExecutionListeners() {
return TestContextSpringFactoriesUtils.loadFactoryImplementations(TestExecutionListener.class);
}

@SuppressWarnings("unchecked")
private List<TestExecutionListener> instantiateListeners(Class<? extends TestExecutionListener>... classes) {
List<TestExecutionListener> listeners = new ArrayList<>(classes.length);
for (Class<? extends TestExecutionListener> listenerClass : classes) {
try {
listeners.add(BeanUtils.instantiateClass(listenerClass));
}
catch (BeanInstantiationException ex) {
Throwable cause = ex.getCause();
if (cause instanceof ClassNotFoundException || cause instanceof NoClassDefFoundError) {
if (logger.isDebugEnabled()) {
logger.debug("""
Skipping candidate %1$s [%2$s] due to a missing dependency. \
Specify custom %1$s classes or make the default %1$s classes \
and their required dependencies available. Offending class: [%3$s]"""
.formatted(TestExecutionListener.class.getSimpleName(), listenerClass.getName(),
cause.getMessage()));
}
}
else {
throw ex;
}
}
}
return listeners;
}

/**
* {@inheritDoc}
*/
Expand Down Expand Up @@ -473,10 +451,15 @@ protected List<ContextCustomizerFactory> getContextCustomizerFactories() {

@SuppressWarnings("unchecked")
private List<ContextCustomizerFactory> instantiateCustomizerFactories(Class<? extends ContextCustomizerFactory>... classes) {
List<ContextCustomizerFactory> factories = new ArrayList<>(classes.length);
for (Class<? extends ContextCustomizerFactory> factoryClass : classes) {
return instantiateComponents(ContextCustomizerFactory.class, classes);
}

@SuppressWarnings("unchecked")
private <T> List<T> instantiateComponents(Class<T> componentType, Class<? extends T>... classes) {
List<T> components = new ArrayList<>(classes.length);
for (Class<? extends T> clazz : classes) {
try {
factories.add(BeanUtils.instantiateClass(factoryClass));
components.add(BeanUtils.instantiateClass(clazz));
}
catch (BeanInstantiationException ex) {
Throwable cause = ex.getCause();
Expand All @@ -486,16 +469,15 @@ private List<ContextCustomizerFactory> instantiateCustomizerFactories(Class<? ex
Skipping candidate %1$s [%2$s] due to a missing dependency. \
Specify custom %1$s classes or make the default %1$s classes \
and their required dependencies available. Offending class: [%3$s]"""
.formatted(ContextCustomizerFactory.class.getSimpleName(), factoryClass.getName(),
cause.getMessage()));
.formatted(componentType.getSimpleName(), clazz.getName(), cause.getMessage()));
}
}
else {
throw ex;
}
}
}
return factories;
return components;
}

/**
Expand Down

0 comments on commit 09b1e5e

Please sign in to comment.