Skip to content

Commit

Permalink
Verify @⁠MockitoBean & @⁠MockitoSpyBean can be used with @⁠ContextHie…
Browse files Browse the repository at this point in the history
…rarchy

See gh-33742
  • Loading branch information
sbrannen committed Oct 18, 2024
1 parent 2aa3f40 commit c0ad9c0
Show file tree
Hide file tree
Showing 3 changed files with 186 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* 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.example;

/**
* Example bean for mocking tests that call the {@link ExampleService}.
*
* @author Phillip Webb
* @since 6.2
*/
public class ExampleServiceCaller {

private final ExampleService service;

public ExampleServiceCaller(ExampleService service) {
this.service = service;
}

public ExampleService getService() {
return this.service;
}

public String sayGreeting() {
return "I say " + this.service.greeting();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
* 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;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.test.context.ContextHierarchy;
import org.springframework.test.context.bean.override.example.ExampleService;
import org.springframework.test.context.bean.override.example.ExampleServiceCaller;
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;

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

/**
* Tests which verify that {@link MockitoBean @MockitoBean} can be used within a
* {@link ContextHierarchy @ContextHierarchy}.
*
* @author Sam Brannen
* @author Phillip Webb
* @since 6.2
* @see MockitoSpyBeanAndContextHierarchyChildTests
*/
@SpringJUnitConfig
public class MockitoBeanAndContextHierarchyParentTests {

@MockitoBean
ExampleService service;

@Autowired
ApplicationContext context;

@BeforeEach
void configureServiceMock() {
given(service.greeting()).willReturn("mock");
}

@Test
void test() {
assertThat(context.getBeanNamesForType(ExampleService.class)).hasSize(1);
assertThat(context.getBeanNamesForType(ExampleServiceCaller.class)).isEmpty();

assertThat(service.greeting()).isEqualTo("mock");
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
/*
* 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;

import org.junit.jupiter.api.Test;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.ContextHierarchy;
import org.springframework.test.context.aot.DisabledInAotMode;
import org.springframework.test.context.bean.override.example.ExampleService;
import org.springframework.test.context.bean.override.example.ExampleServiceCaller;

import static org.assertj.core.api.Assertions.assertThat;

/**
* Tests which verify that {@link MockitoBean @MockitoBean} and
* {@link MockitoSpyBean @MockitoSpyBean} can be used within a
* {@link ContextHierarchy @ContextHierarchy}.
*
* @author Sam Brannen
* @author Phillip Webb
* @since 6.2
* @see MockitoBeanAndContextHierarchyParentTests
*/
@ContextHierarchy(@ContextConfiguration)
@DisabledInAotMode // @ContextHierarchy is not supported in AOT.
public class MockitoSpyBeanAndContextHierarchyChildTests extends MockitoBeanAndContextHierarchyParentTests {

@MockitoSpyBean
ExampleServiceCaller serviceCaller;

@Autowired
ApplicationContext context;


@Test
@Override
void test() {
assertThat(context).as("child ApplicationContext").isNotNull();
assertThat(context.getParent()).as("parent ApplicationContext").isNotNull();
assertThat(context.getParent().getParent()).as("grandparent ApplicationContext").isNull();

ApplicationContext parentContext = context.getParent();
assertThat(parentContext.getBeanNamesForType(ExampleService.class)).hasSize(1);
assertThat(parentContext.getBeanNamesForType(ExampleServiceCaller.class)).isEmpty();

assertThat(context.getBeanNamesForType(ExampleService.class)).hasSize(1);
assertThat(context.getBeanNamesForType(ExampleServiceCaller.class)).hasSize(1);

assertThat(service.greeting()).isEqualTo("mock");
assertThat(serviceCaller.sayGreeting()).isEqualTo("I say mock");
}


@Configuration(proxyBeanMethods = false)
static class ChildConfig {

@Bean
ExampleServiceCaller serviceCaller(ExampleService service) {
return new ExampleServiceCaller(service);
}
}

}

0 comments on commit c0ad9c0

Please sign in to comment.