Upgrade to Mockito 5.0

This commit is contained in:
Sam Brannen
2023-01-17 14:31:11 +01:00
parent ebdc82b86b
commit ad5c636aff
5 changed files with 43 additions and 27 deletions

View File

@@ -1928,8 +1928,8 @@ public class AutowiredAnnotationBeanPostProcessorTests {
bf.registerBeanDefinition("annotatedBean", bd);
RootBeanDefinition rbd = new RootBeanDefinition();
rbd.setBeanClassName(Mockito.class.getName());
rbd.setFactoryMethodName("mock");
rbd.setBeanClassName(getClass().getName());
rbd.setFactoryMethodName("createMockitoMock");
// TypedStringValue used to be equivalent to an XML-defined argument String
rbd.getConstructorArgumentValues().addGenericArgumentValue(new TypedStringValue(Repository.class.getName()));
bf.registerBeanDefinition("repo", rbd);
@@ -1952,6 +1952,15 @@ public class AutowiredAnnotationBeanPostProcessorTests {
assertThat(bean.stringRepositoryMap.get("repo")).isSameAs(repo);
}
/**
* Mimics and delegates to {@link Mockito#mock(Class)} -- created here to avoid factory
* method resolution issues caused by the introduction of {@code Mockito.mock(T...)}
* in Mockito 4.10.
*/
public static <T> T createMockitoMock(Class<T> classToMock) {
return Mockito.mock(classToMock);
}
@Test
public void testGenericsBasedMethodInjection() {
RootBeanDefinition bd = new RootBeanDefinition(RepositoryMethodInjectionBean.class);

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2022 the original author or authors.
* Copyright 2002-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -659,14 +659,29 @@ class BeanFactoryGenericsTests {
*/
@Test
void parameterizedStaticFactoryMethod() {
RootBeanDefinition rbd = new RootBeanDefinition(Mockito.class);
rbd.setFactoryMethodName("mock");
RootBeanDefinition rbd = new RootBeanDefinition(getClass());
rbd.setFactoryMethodName("createMockitoMock");
rbd.getConstructorArgumentValues().addGenericArgumentValue(Runnable.class);
assertRunnableMockFactory(rbd);
}
@Test
void parameterizedStaticFactoryMethodWithWrappedClassName() {
RootBeanDefinition rbd = new RootBeanDefinition();
rbd.setBeanClassName(getClass().getName());
rbd.setFactoryMethodName("createMockitoMock");
// TypedStringValue is used as an equivalent to an XML-defined argument String
rbd.getConstructorArgumentValues().addGenericArgumentValue(new TypedStringValue(Runnable.class.getName()));
assertRunnableMockFactory(rbd);
}
private void assertRunnableMockFactory(RootBeanDefinition rbd) {
DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
bf.registerBeanDefinition("mock", rbd);
assertThat(bf.getType("mock")).isEqualTo(Runnable.class);
assertThat(bf.isTypeMatch("mock", Runnable.class)).isTrue();
assertThat(bf.getType("mock")).isEqualTo(Runnable.class);
Map<String, Runnable> beans = bf.getBeansOfType(Runnable.class);
assertThat(beans).hasSize(1);
@@ -725,25 +740,6 @@ class BeanFactoryGenericsTests {
assertThat(beans).hasSize(1);
}
@Test
void parameterizedInstanceFactoryMethodWithWrappedClassName() {
DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
RootBeanDefinition rbd = new RootBeanDefinition();
rbd.setBeanClassName(Mockito.class.getName());
rbd.setFactoryMethodName("mock");
// TypedStringValue used to be equivalent to an XML-defined argument String
rbd.getConstructorArgumentValues().addGenericArgumentValue(new TypedStringValue(Runnable.class.getName()));
bf.registerBeanDefinition("mock", rbd);
assertThat(bf.isTypeMatch("mock", Runnable.class)).isTrue();
assertThat(bf.isTypeMatch("mock", Runnable.class)).isTrue();
assertThat(bf.getType("mock")).isEqualTo(Runnable.class);
assertThat(bf.getType("mock")).isEqualTo(Runnable.class);
Map<String, Runnable> beans = bf.getBeansOfType(Runnable.class);
assertThat(beans).hasSize(1);
}
@Test
void parameterizedInstanceFactoryMethodWithInvalidClassName() {
DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
@@ -945,6 +941,16 @@ class BeanFactoryGenericsTests {
}
/**
* Mimics and delegates to {@link Mockito#mock(Class)} -- created here to avoid factory
* method resolution issues caused by the introduction of {@code Mockito.mock(T...)}
* in Mockito 4.10.
*/
public static <T> T createMockitoMock(Class<T> classToMock) {
return Mockito.mock(classToMock);
}
@SuppressWarnings("serial")
public static class NamedUrlList extends ArrayList<URL> {
}