Similar to @MockBean, Spring Boot provides @SpyBean annotation to apply Mockito spies to a Spring ApplicationContext.
Example
Example Spring boot application
package com.logicbig.example;
public class HelloService {
public String getMessage(){
return null;
}
public String getFormattedMessage() {
return "Hi there! " + getMessage();
}
}
package com.logicbig.example;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component
public class MyHelloProcessor {
@Autowired
private HelloService helloService;
public String getMessage() {
return helloService.getMessage();
}
public String getFormattedMessage() {
return helloService.getFormattedMessage();
}
}
Using @SpyBean
package com.logicbig.example;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mockito;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.mock.mockito.SpyBean;
import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
@SpringBootTest
public class MyHelloProcessorTest {
@Autowired
private MyHelloProcessor myHelloProcessor;
@SpyBean
private HelloService helloService;
@Test
public void testSayHi() {
Mockito.when(helloService.getMessage()).thenReturn("test message.");
String message = myHelloProcessor.getMessage();
Assert.assertEquals(message, "test message.");
String formattedMessage = myHelloProcessor.getFormattedMessage();
Assert.assertEquals(formattedMessage, "Hi there! test message.");
}
}
D:\spring-boot-using-annotation-spybean>mvn test -Dtest=MyHelloProcessorTest.java [INFO] Scanning for projects... [INFO] [INFO] -----< com.logicbig.example:spring-boot-using-annotation-spybean >------ [INFO] Building spring-boot-using-annotation-spybean 1.0-SNAPSHOT [INFO] --------------------------------[ jar ]--------------------------------- [INFO] [INFO] --- maven-resources-plugin:3.1.0:resources (default-resources) @ spring-boot-using-annotation-spybean --- [INFO] Using 'UTF-8' encoding to copy filtered resources. [INFO] [INFO] --- maven-compiler-plugin:3.8.1:compile (default-compile) @ spring-boot-using-annotation-spybean --- [INFO] Changes detected - recompiling the module! [INFO] [INFO] --- maven-resources-plugin:3.1.0:testResources (default-testResources) @ spring-boot-using-annotation-spybean --- [INFO] Using 'UTF-8' encoding to copy filtered resources. [INFO] [INFO] --- maven-compiler-plugin:3.8.1:testCompile (default-testCompile) @ spring-boot-using-annotation-spybean --- [INFO] Changes detected - recompiling the module! [INFO] [INFO] --- maven-surefire-plugin:2.22.2:test (default-test) @ spring-boot-using-annotation-spybean --- [INFO] [INFO] ------------------------------------------------------- [INFO] T E S T S [INFO] ------------------------------------------------------- 00:53:46.342 [main] DEBUG org.springframework.test.context.junit4.SpringJUnit4ClassRunner - SpringJUnit4ClassRunner constructor called with [class com.logicbig.example.MyHelloProcessorTest] 00:53:46.345 [main] DEBUG org.springframework.test.context.BootstrapUtils - Instantiating CacheAwareContextLoaderDelegate from class [org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate] 00:53:46.352 [main] DEBUG org.springframework.test.context.BootstrapUtils - Instantiating BootstrapContext using constructor [public org.springframework.test.context.support.DefaultBootstrapContext(java.lang.Class,org.springframework.test.context.CacheAwareContextLoaderDelegate)] 00:53:46.379 [main] DEBUG org.springframework.test.context.BootstrapUtils - Instantiating TestContextBootstrapper for test class [com.logicbig.example.MyHelloProcessorTest] from class [org.springframework.boot.test.context.SpringBootTestContextBootstrapper] 00:53:46.390 [main] INFO org.springframework.boot.test.context.SpringBootTestContextBootstrapper - Neither @ContextConfiguration nor @ContextHierarchy found for test class [com.logicbig.example.MyHelloProcessorTest], using SpringBootContextLoader 00:53:46.394 [main] DEBUG org.springframework.test.context.support.AbstractContextLoader - Did not detect default resource location for test class [com.logicbig.example.MyHelloProcessorTest]: class path resource [com/logicbig/example/MyHelloProcessorTest-context.xml] does not exist 00:53:46.394 [main] DEBUG org.springframework.test.context.support.AbstractContextLoader - Did not detect default resource location for test class [com.logicbig.example.MyHelloProcessorTest]: class path resource [com/logicbig/example/MyHelloProcessorTestContext.groovy] does not exist 00:53:46.394 [main] INFO org.springframework.test.context.support.AbstractContextLoader - Could not detect default resource locations for test class [com.logicbig.example.MyHelloProcessorTest]: no resource found for suffixes {-context.xml, Context.groovy}. 00:53:46.395 [main] INFO org.springframework.test.context.support.AnnotationConfigContextLoaderUtils - Could not detect default configuration classes for test class [com.logicbig.example.MyHelloProcessorTest]: MyHelloProcessorTest does not declare any static, non-private, non-final, nested classes annotated with @Configuration. 00:53:46.431 [main] DEBUG org.springframework.test.context.support.ActiveProfilesUtils - Could not find an 'annotation declaring class' for annotation type [org.springframework.test.context.ActiveProfiles] and class [com.logicbig.example.MyHelloProcessorTest] 00:53:46.480 [main] INFO org.springframework.boot.test.context.SpringBootTestContextBootstrapper - Found @SpringBootConfiguration com.logicbig.example.AppMain for test class com.logicbig.example.MyHelloProcessorTest 00:53:46.553 [main] DEBUG org.springframework.boot.test.context.SpringBootTestContextBootstrapper - @TestExecutionListeners is not present for class [com.logicbig.example.MyHelloProcessorTest]: using defaults. 00:53:46.554 [main] INFO org.springframework.boot.test.context.SpringBootTestContextBootstrapper - Loaded default TestExecutionListener class names from location [META-INF/spring.factories]: [org.springframework.boot.test.mock.mockito.MockitoTestExecutionListener, org.springframework.boot.test.mock.mockito.ResetMocksTestExecutionListener, org.springframework.boot.test.autoconfigure.restdocs.RestDocsTestExecutionListener, org.springframework.boot.test.autoconfigure.web.client.MockRestServiceServerResetTestExecutionListener, org.springframework.boot.test.autoconfigure.web.servlet.MockMvcPrintOnlyOnFailureTestExecutionListener, org.springframework.boot.test.autoconfigure.web.servlet.WebDriverTestExecutionListener, org.springframework.boot.test.autoconfigure.webservices.client.MockWebServiceServerTestExecutionListener, org.springframework.test.context.web.ServletTestExecutionListener, org.springframework.test.context.support.DirtiesContextBeforeModesTestExecutionListener, org.springframework.test.context.support.DependencyInjectionTestExecutionListener, org.springframework.test.context.support.DirtiesContextTestExecutionListener, org.springframework.test.context.transaction.TransactionalTestExecutionListener, org.springframework.test.context.jdbc.SqlScriptsTestExecutionListener, org.springframework.test.context.event.EventPublishingTestExecutionListener] 00:53:46.562 [main] DEBUG org.springframework.boot.test.context.SpringBootTestContextBootstrapper - Skipping candidate TestExecutionListener [org.springframework.test.context.transaction.TransactionalTestExecutionListener] due to a missing dependency. Specify custom listener classes or make the default listener classes and their required dependencies available. Offending class: [org/springframework/transaction/interceptor/TransactionAttributeSource] 00:53:46.563 [main] DEBUG org.springframework.boot.test.context.SpringBootTestContextBootstrapper - Skipping candidate TestExecutionListener [org.springframework.test.context.jdbc.SqlScriptsTestExecutionListener] due to a missing dependency. Specify custom listener classes or make the default listener classes and their required dependencies available. Offending class: [org/springframework/transaction/interceptor/TransactionAttribute] 00:53:46.564 [main] INFO org.springframework.boot.test.context.SpringBootTestContextBootstrapper - Using TestExecutionListeners: [org.springframework.test.context.web.ServletTestExecutionListener@48b67364, org.springframework.test.context.support.DirtiesContextBeforeModesTestExecutionListener@189cbd7c, org.springframework.boot.test.mock.mockito.MockitoTestExecutionListener@7bf3a5d8, org.springframework.boot.test.autoconfigure.SpringBootDependencyInjectionTestExecutionListener@42e25b0b, org.springframework.test.context.support.DirtiesContextTestExecutionListener@39b43d60, org.springframework.test.context.event.EventPublishingTestExecutionListener@44be0077, org.springframework.boot.test.mock.mockito.ResetMocksTestExecutionListener@2205a05d, org.springframework.boot.test.autoconfigure.restdocs.RestDocsTestExecutionListener@72ef8d15, org.springframework.boot.test.autoconfigure.web.client.MockRestServiceServerResetTestExecutionListener@6aa8e115, org.springframework.boot.test.autoconfigure.web.servlet.MockMvcPrintOnlyOnFailureTestExecutionListener@5e21e98f, org.springframework.boot.test.autoconfigure.web.servlet.WebDriverTestExecutionListener@51a9ad5e, org.springframework.boot.test.autoconfigure.webservices.client.MockWebServiceServerTestExecutionListener@5f20155b] 00:53:46.565 [main] DEBUG org.springframework.test.annotation.ProfileValueUtils - Retrieved @ProfileValueSourceConfiguration [null] for test class [com.logicbig.example.MyHelloProcessorTest] 00:53:46.566 [main] DEBUG org.springframework.test.annotation.ProfileValueUtils - Retrieved ProfileValueSource type [class org.springframework.test.annotation.SystemProfileValueSource] for class [com.logicbig.example.MyHelloProcessorTest] 00:53:46.567 [main] DEBUG org.springframework.test.annotation.ProfileValueUtils - Retrieved @ProfileValueSourceConfiguration [null] for test class [com.logicbig.example.MyHelloProcessorTest] 00:53:46.567 [main] DEBUG org.springframework.test.annotation.ProfileValueUtils - Retrieved ProfileValueSource type [class org.springframework.test.annotation.SystemProfileValueSource] for class [com.logicbig.example.MyHelloProcessorTest] 00:53:46.571 [main] DEBUG org.springframework.test.annotation.ProfileValueUtils - Retrieved @ProfileValueSourceConfiguration [null] for test class [com.logicbig.example.MyHelloProcessorTest] 00:53:46.571 [main] DEBUG org.springframework.test.annotation.ProfileValueUtils - Retrieved ProfileValueSource type [class org.springframework.test.annotation.SystemProfileValueSource] for class [com.logicbig.example.MyHelloProcessorTest] 00:53:46.606 [main] DEBUG org.springframework.test.context.junit4.SpringJUnit4ClassRunner - SpringJUnit4ClassRunner constructor called with [class com.logicbig.example.MyHelloProcessorTest] 00:53:46.606 [main] DEBUG org.springframework.test.context.BootstrapUtils - Instantiating CacheAwareContextLoaderDelegate from class [org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate] 00:53:46.606 [main] DEBUG org.springframework.test.context.BootstrapUtils - Instantiating BootstrapContext using constructor [public org.springframework.test.context.support.DefaultBootstrapContext(java.lang.Class,org.springframework.test.context.CacheAwareContextLoaderDelegate)] 00:53:46.607 [main] DEBUG org.springframework.test.context.BootstrapUtils - Instantiating TestContextBootstrapper for test class [com.logicbig.example.MyHelloProcessorTest] from class [org.springframework.boot.test.context.SpringBootTestContextBootstrapper] 00:53:46.608 [main] INFO org.springframework.boot.test.context.SpringBootTestContextBootstrapper - Neither @ContextConfiguration nor @ContextHierarchy found for test class [com.logicbig.example.MyHelloProcessorTest], using SpringBootContextLoader 00:53:46.610 [main] DEBUG org.springframework.test.context.support.AbstractContextLoader - Did not detect default resource location for test class [com.logicbig.example.MyHelloProcessorTest]: class path resource [com/logicbig/example/MyHelloProcessorTest-context.xml] does not exist 00:53:46.610 [main] DEBUG org.springframework.test.context.support.AbstractContextLoader - Did not detect default resource location for test class [com.logicbig.example.MyHelloProcessorTest]: class path resource [com/logicbig/example/MyHelloProcessorTestContext.groovy] does not exist 00:53:46.610 [main] INFO org.springframework.test.context.support.AbstractContextLoader - Could not detect default resource locations for test class [com.logicbig.example.MyHelloProcessorTest]: no resource found for suffixes {-context.xml, Context.groovy}. 00:53:46.611 [main] INFO org.springframework.test.context.support.AnnotationConfigContextLoaderUtils - Could not detect default configuration classes for test class [com.logicbig.example.MyHelloProcessorTest]: MyHelloProcessorTest does not declare any static, non-private, non-final, nested classes annotated with @Configuration. 00:53:46.613 [main] DEBUG org.springframework.test.context.support.ActiveProfilesUtils - Could not find an 'annotation declaring class' for annotation type [org.springframework.test.context.ActiveProfiles] and class [com.logicbig.example.MyHelloProcessorTest] 00:53:46.614 [main] INFO org.springframework.boot.test.context.SpringBootTestContextBootstrapper - Found @SpringBootConfiguration com.logicbig.example.AppMain for test class com.logicbig.example.MyHelloProcessorTest 00:53:46.617 [main] DEBUG org.springframework.boot.test.context.SpringBootTestContextBootstrapper - @TestExecutionListeners is not present for class [com.logicbig.example.MyHelloProcessorTest]: using defaults. 00:53:46.617 [main] INFO org.springframework.boot.test.context.SpringBootTestContextBootstrapper - Loaded default TestExecutionListener class names from location [META-INF/spring.factories]: [org.springframework.boot.test.mock.mockito.MockitoTestExecutionListener, org.springframework.boot.test.mock.mockito.ResetMocksTestExecutionListener, org.springframework.boot.test.autoconfigure.restdocs.RestDocsTestExecutionListener, org.springframework.boot.test.autoconfigure.web.client.MockRestServiceServerResetTestExecutionListener, org.springframework.boot.test.autoconfigure.web.servlet.MockMvcPrintOnlyOnFailureTestExecutionListener, org.springframework.boot.test.autoconfigure.web.servlet.WebDriverTestExecutionListener, org.springframework.boot.test.autoconfigure.webservices.client.MockWebServiceServerTestExecutionListener, org.springframework.test.context.web.ServletTestExecutionListener, org.springframework.test.context.support.DirtiesContextBeforeModesTestExecutionListener, org.springframework.test.context.support.DependencyInjectionTestExecutionListener, org.springframework.test.context.support.DirtiesContextTestExecutionListener, org.springframework.test.context.transaction.TransactionalTestExecutionListener, org.springframework.test.context.jdbc.SqlScriptsTestExecutionListener, org.springframework.test.context.event.EventPublishingTestExecutionListener] 00:53:46.618 [main] DEBUG org.springframework.boot.test.context.SpringBootTestContextBootstrapper - Skipping candidate TestExecutionListener [org.springframework.test.context.transaction.TransactionalTestExecutionListener] due to a missing dependency. Specify custom listener classes or make the default listener classes and their required dependencies available. Offending class: [org/springframework/transaction/interceptor/TransactionAttributeSource] 00:53:46.619 [main] DEBUG org.springframework.boot.test.context.SpringBootTestContextBootstrapper - Skipping candidate TestExecutionListener [org.springframework.test.context.jdbc.SqlScriptsTestExecutionListener] due to a missing dependency. Specify custom listener classes or make the default listener classes and their required dependencies available. Offending class: [org/springframework/transaction/interceptor/TransactionAttribute] 00:53:46.619 [main] INFO org.springframework.boot.test.context.SpringBootTestContextBootstrapper - Using TestExecutionListeners: [org.springframework.test.context.web.ServletTestExecutionListener@57576994, org.springframework.test.context.support.DirtiesContextBeforeModesTestExecutionListener@616ac46a, org.springframework.boot.test.mock.mockito.MockitoTestExecutionListener@67b9b51a, org.springframework.boot.test.autoconfigure.SpringBootDependencyInjectionTestExecutionListener@1205bd62, org.springframework.test.context.support.DirtiesContextTestExecutionListener@7ef27d7f, org.springframework.test.context.event.EventPublishingTestExecutionListener@490caf5f, org.springframework.boot.test.mock.mockito.ResetMocksTestExecutionListener@6337c201, org.springframework.boot.test.autoconfigure.restdocs.RestDocsTestExecutionListener@5c669da8, org.springframework.boot.test.autoconfigure.web.client.MockRestServiceServerResetTestExecutionListener@31920ade, org.springframework.boot.test.autoconfigure.web.servlet.MockMvcPrintOnlyOnFailureTestExecutionListener@1d483de4, org.springframework.boot.test.autoconfigure.web.servlet.WebDriverTestExecutionListener@4032d386, org.springframework.boot.test.autoconfigure.webservices.client.MockWebServiceServerTestExecutionListener@28d18df5] 00:53:46.619 [main] DEBUG org.springframework.test.annotation.ProfileValueUtils - Retrieved @ProfileValueSourceConfiguration [null] for test class [com.logicbig.example.MyHelloProcessorTest] 00:53:46.619 [main] DEBUG org.springframework.test.annotation.ProfileValueUtils - Retrieved ProfileValueSource type [class org.springframework.test.annotation.SystemProfileValueSource] for class [com.logicbig.example.MyHelloProcessorTest] 00:53:46.620 [main] DEBUG org.springframework.test.annotation.ProfileValueUtils - Retrieved @ProfileValueSourceConfiguration [null] for test class [com.logicbig.example.MyHelloProcessorTest] 00:53:46.620 [main] DEBUG org.springframework.test.annotation.ProfileValueUtils - Retrieved ProfileValueSource type [class org.springframework.test.annotation.SystemProfileValueSource] for class [com.logicbig.example.MyHelloProcessorTest] 00:53:46.620 [main] DEBUG org.springframework.test.annotation.ProfileValueUtils - Retrieved @ProfileValueSourceConfiguration [null] for test class [com.logicbig.example.MyHelloProcessorTest] 00:53:46.620 [main] DEBUG org.springframework.test.annotation.ProfileValueUtils - Retrieved ProfileValueSource type [class org.springframework.test.annotation.SystemProfileValueSource] for class [com.logicbig.example.MyHelloProcessorTest] 00:53:46.667 [main] DEBUG org.springframework.test.annotation.ProfileValueUtils - Retrieved @ProfileValueSourceConfiguration [null] for test class [com.logicbig.example.MyHelloProcessorTest] 00:53:46.667 [main] DEBUG org.springframework.test.annotation.ProfileValueUtils - Retrieved ProfileValueSource type [class org.springframework.test.annotation.SystemProfileValueSource] for class [com.logicbig.example.MyHelloProcessorTest] [INFO] Running com.logicbig.example.MyHelloProcessorTest 00:53:46.669 [main] DEBUG org.springframework.test.annotation.ProfileValueUtils - Retrieved @ProfileValueSourceConfiguration [null] for test class [com.logicbig.example.MyHelloProcessorTest] 00:53:46.670 [main] DEBUG org.springframework.test.annotation.ProfileValueUtils - Retrieved ProfileValueSource type [class org.springframework.test.annotation.SystemProfileValueSource] for class [com.logicbig.example.MyHelloProcessorTest] 00:53:46.670 [main] DEBUG org.springframework.test.annotation.ProfileValueUtils - Retrieved @ProfileValueSourceConfiguration [null] for test class [com.logicbig.example.MyHelloProcessorTest] 00:53:46.670 [main] DEBUG org.springframework.test.annotation.ProfileValueUtils - Retrieved ProfileValueSource type [class org.springframework.test.annotation.SystemProfileValueSource] for class [com.logicbig.example.MyHelloProcessorTest] 00:53:46.674 [main] DEBUG org.springframework.test.context.support.AbstractDirtiesContextTestExecutionListener - Before test class: context [DefaultTestContext@56928307 testClass = MyHelloProcessorTest, testInstance = [null], testMethod = [null], testException = [null], mergedContextConfiguration = [WebMergedContextConfiguration@3899782c testClass = MyHelloProcessorTest, locations = '{}', classes = '{class com.logicbig.example.AppMain}', contextInitializerClasses = '[]', activeProfiles = '{}', propertySourceLocations = '{}', propertySourceProperties = '{org.springframework.boot.test.context.SpringBootTestContextBootstrapper=true}', contextCustomizers = set[org.springframework.boot.test.context.filter.ExcludeFilterContextCustomizer@fbd1f6, org.springframework.boot.test.json.DuplicateJsonObjectContextCustomizerFactory$DuplicateJsonObjectContextCustomizer@7a8c8dcf, org.springframework.boot.test.mock.mockito.MockitoContextCustomizer@5e192b6, org.springframework.boot.test.web.client.TestRestTemplateContextCustomizer@17f7cd29, org.springframework.boot.test.autoconfigure.properties.PropertyMappingContextCustomizer@0, org.springframework.boot.test.autoconfigure.web.servlet.WebDriverContextCustomizerFactory$Customizer@10aa41f2, org.springframework.boot.test.context.SpringBootTestArgs@1], resourceBasePath = 'src/main/webapp', contextLoader = 'org.springframework.boot.test.context.SpringBootContextLoader', parent = [null]], attributes = map['org.springframework.test.context.web.ServletTestExecutionListener.activateListener' -> true]], class annotated with @DirtiesContext [false] with mode [null]. 00:53:46.675 [main] DEBUG org.springframework.test.annotation.ProfileValueUtils - Retrieved @ProfileValueSourceConfiguration [null] for test class [com.logicbig.example.MyHelloProcessorTest] 00:53:46.676 [main] DEBUG org.springframework.test.annotation.ProfileValueUtils - Retrieved ProfileValueSource type [class org.springframework.test.annotation.SystemProfileValueSource] for class [com.logicbig.example.MyHelloProcessorTest] 00:53:46.695 [main] DEBUG org.springframework.test.context.support.TestPropertySourceUtils - Adding inlined properties to environment: {spring.jmx.enabled=false, org.springframework.boot.test.context.SpringBootTestContextBootstrapper=true}
. ____ _ __ _ _ /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \ ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \ \\/ ___)| |_)| | | | | || (_| | ) ) ) ) ' |____| .__|_| |_|_| |_\__, | / / / / =========|_|==============|___/=/_/_/_/ :: Spring Boot :: (v2.3.3.RELEASE)
2020-10-27 00:53:46.877 INFO 9356 --- [ main] c.logicbig.example.MyHelloProcessorTest : No active profile set, falling back to default profiles: default 2020-10-27 00:53:48.236 INFO 9356 --- [ main] o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService 'applicationTaskExecutor' 2020-10-27 00:53:48.491 INFO 9356 --- [ main] c.logicbig.example.MyHelloProcessorTest : Started MyHelloProcessorTest in 1.787 seconds (JVM running for 2.559) [INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 1.869 s - in com.logicbig.example.MyHelloProcessorTest 2020-10-27 00:53:48.557 INFO 9356 --- [extShutdownHook] o.s.s.concurrent.ThreadPoolTaskExecutor : Shutting down ExecutorService 'applicationTaskExecutor' [INFO] [INFO] Results: [INFO] [INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0 [INFO] [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 5.695 s [INFO] Finished at: 2020-10-27T00:53:48-05:00 [INFO] ------------------------------------------------------------------------
Example ProjectDependencies and Technologies Used: - Spring Boot 2.3.3.RELEASE
- spring-boot-starter-web : Starter for building web, including RESTful, applications using Spring MVC. Uses Tomcat as the default embedded container.
- spring-boot-starter-test : Starter for testing Spring Boot applications with libraries including JUnit, Hamcrest and Mockito.
- JDK 1.8
- Maven 3.5.4
|
|