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

@ -19,7 +19,7 @@ dependencies {
api(platform("org.jetbrains.kotlinx:kotlinx-coroutines-bom:1.6.4"))
api(platform("org.jetbrains.kotlinx:kotlinx-serialization-bom:1.4.0"))
api(platform("org.junit:junit-bom:5.9.2"))
api(platform("org.mockito:mockito-bom:4.9.0")) // spring-beans tests fail with 4.10+
api(platform("org.mockito:mockito-bom:5.0.0"))
constraints {
api("com.fasterxml:aalto-xml:1.3.1")

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> {
}

View File

@ -6,7 +6,7 @@
https://www.springframework.org/schema/beans/spring-beans-3.1.xsd">
<bean id="personDao" class="org.mockito.Mockito" factory-method="mock">
<constructor-arg value="org.springframework.test.web.servlet.samples.context.PersonDao" />
<constructor-arg type="java.lang.Class" value="org.springframework.test.web.servlet.samples.context.PersonDao" />
</bean>
</beans>

View File

@ -40,6 +40,7 @@ dependencies {
testImplementation("jakarta.validation:jakarta.validation-api")
testImplementation("io.reactivex.rxjava3:rxjava")
testImplementation("io.projectreactor:reactor-test")
testImplementation("io.projectreactor.tools:blockhound") // required by Mockito 5's default MockMaker in order to mock ReactiveAdapterRegistry which "contains" SpringCoreBlockHoundIntegration
testImplementation("io.micrometer:micrometer-observation-test")
testImplementation("io.undertow:undertow-core")
testImplementation("org.apache.tomcat.embed:tomcat-embed-core")