Skip to content

Commit

Permalink
Verify @⁠MockitoBean can be used with generics and parameterized types
Browse files Browse the repository at this point in the history
  • Loading branch information
sbrannen committed Oct 24, 2024
1 parent efda3f0 commit ba8024d
Show file tree
Hide file tree
Showing 2 changed files with 122 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/*
* Copyright 2002-2024 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.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.springframework.test.context.bean.override.mockito.integration;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.test.context.bean.override.mockito.MockitoBean;
import org.springframework.test.context.bean.override.mockito.integration.AbstractMockitoBeanAndGenericsIntegrationTests.Something;
import org.springframework.test.context.bean.override.mockito.integration.AbstractMockitoBeanAndGenericsIntegrationTests.Thing;
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;

/**
* Abstract base class for tests for {@link MockitoBean @MockitoBean} with generics.
*
* @param <T> type of thing
* @param <S> type of something
* @author Madhura Bhave
* @author Sam Brannen
* @since 6.2
* @see MockitoBeanAndGenericsIntegrationTests
*/
@SpringJUnitConfig
abstract class AbstractMockitoBeanAndGenericsIntegrationTests<T extends Thing<S>, S extends Something> {

@Autowired
T thing;

@MockitoBean
S something;


static class Something {
String speak() {
return "Hi";
}
}

static class SomethingImpl extends Something {
}

abstract static class Thing<S extends Something> {

@Autowired
private S something;

S getSomething() {
return this.something;
}
}

static class ThingImpl extends Thing<SomethingImpl> {
}

@Configuration(proxyBeanMethods = false)
static class Config {

@Bean
ThingImpl thing() {
return new ThingImpl();
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* Copyright 2012-2024 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.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.springframework.test.context.bean.override.mockito.integration;

import org.junit.jupiter.api.Test;

import org.springframework.test.context.bean.override.mockito.integration.AbstractMockitoBeanAndGenericsIntegrationTests.SomethingImpl;
import org.springframework.test.context.bean.override.mockito.integration.AbstractMockitoBeanAndGenericsIntegrationTests.ThingImpl;

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.BDDMockito.when;

/**
* Concrete implementation of {@link AbstractMockitoBeanAndGenericsIntegrationTests}.
*
* @author Madhura Bhave
* @author Sam Brannen
* @since 6.2
*/
class MockitoBeanAndGenericsIntegrationTests extends AbstractMockitoBeanAndGenericsIntegrationTests<ThingImpl, SomethingImpl> {

@Test
void mockitoBeanShouldResolveConcreteType() {
assertThat(something).isExactlyInstanceOf(SomethingImpl.class);

when(something.speak()).thenReturn("Hola");
assertThat(thing.getSomething().speak()).isEqualTo("Hola");
}

}

0 comments on commit ba8024d

Please sign in to comment.