Skip to content

Commit

Permalink
Avoid error when mockito lib is not in claspath/modulepath
Browse files Browse the repository at this point in the history
Signed-off-by: Jorge Bescos Gascon <jorge.bescos.gascon@oracle.com>
  • Loading branch information
jbescos committed Apr 23, 2024
1 parent cd1b59b commit bcc23b8
Showing 1 changed file with 11 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -524,8 +524,6 @@ private static class AddBeansExtension implements Extension {
private final HashMap<String, Annotation> socketAnnotations = new HashMap<>();
private final Set<Class<?>> mocks = new HashSet<>();

private MockSettings mockSettings;

private AddBeansExtension(Class<?> testClass, List<AddBean> addBeans) {
this.testClass = testClass;
this.addBeans = addBeans;
Expand All @@ -543,20 +541,6 @@ void processSocketInjectionPoints(@Observes ProcessInjectionPoint<?, WebTarget>
}
}

private MockSettings mockSettings(BeanManager beanManager) {
if (mockSettings == null) {
Set<Bean<?>> beans = beanManager.getBeans(MockSettings.class);
if (!beans.isEmpty()) {
Bean<?> bean = beans.iterator().next();
mockSettings = (MockSettings) beanManager.getReference(bean, MockSettings.class,
beanManager.createCreationalContext(null));
} else {
mockSettings = Mockito.withSettings();
}
}
return mockSettings;
}

void processMockBean(@Observes @WithAnnotations(MockBean.class) ProcessAnnotatedType<?> obj) throws Exception {
var configurator = obj.configureAnnotatedType();
configurator.fields().forEach(field -> {
Expand Down Expand Up @@ -609,7 +593,17 @@ void registerOtherBeans(@Observes AfterBeanDiscovery event, BeanManager beanMana
.addType(type)
.scope(ApplicationScoped.class)
.alternative(true)
.createWith(inst -> Mockito.mock(type, mockSettings(beanManager)))
.createWith(inst -> {
Set<Bean<?>> beans = beanManager.getBeans(MockSettings.class);
if (!beans.isEmpty()) {
Bean<?> bean = beans.iterator().next();
MockSettings mockSettings = (MockSettings) beanManager.getReference(bean, MockSettings.class,
beanManager.createCreationalContext(null));
return Mockito.mock(type, mockSettings);
} else {
return Mockito.mock(type);
}
})
.priority(0);
});
}
Expand Down

0 comments on commit bcc23b8

Please sign in to comment.