diff --git a/src/main/java/com/teketik/test/mockinbean/BeanUtils.java b/src/main/java/com/teketik/test/mockinbean/BeanUtils.java index 388c368..7dd707f 100644 --- a/src/main/java/com/teketik/test/mockinbean/BeanUtils.java +++ b/src/main/java/com/teketik/test/mockinbean/BeanUtils.java @@ -75,7 +75,7 @@ static Field findField(Class clazz, @Nullable String name, Class type) { } else { results[1] = Boolean.FALSE; //multiple matching fields } - }, field -> field.getType().equals(type)); + }, field -> field.getType().isAssignableFrom(type)); if (results[0] != null) { Assert.isTrue( !(results[0] instanceof Boolean), diff --git a/src/test/java/com/teketik/test/mockinbean/test/InterfaceImplementation1Test.java b/src/test/java/com/teketik/test/mockinbean/test/InterfaceImplementation1Test.java new file mode 100644 index 0000000..9ad5606 --- /dev/null +++ b/src/test/java/com/teketik/test/mockinbean/test/InterfaceImplementation1Test.java @@ -0,0 +1,35 @@ +package com.teketik.test.mockinbean.test; + + +import com.teketik.test.mockinbean.MockInBean; +import com.teketik.test.mockinbean.test.InterfaceImplementationTestConfig.LoggingService; +import com.teketik.test.mockinbean.test.InterfaceImplementationTestConfig.ProviderServiceImpl; + +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; +import org.mockito.Mockito; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.annotation.Import; + +@Import(InterfaceImplementationTestConfig.class) +public class InterfaceImplementation1Test extends BaseTest { + + @Autowired + protected LoggingService loggingService; + + @MockInBean(LoggingService.class) + private ProviderServiceImpl providerService; + + @MockInBean(LoggingService.class) + private ProviderServiceImpl providerServiceImpl; + + @Test + public void test() { + Mockito.when(providerService.provideValue()).thenReturn("mocked value"); + Mockito.when(providerServiceImpl.provideValue()).thenReturn("mocked value 2"); + + Assertions.assertEquals("mocked value", loggingService.logCurrentValue()); + Assertions.assertEquals("mocked value 2", loggingService.logCurrentValueWithImpl()); + } + +} diff --git a/src/test/java/com/teketik/test/mockinbean/test/InterfaceImplementation2Test.java b/src/test/java/com/teketik/test/mockinbean/test/InterfaceImplementation2Test.java new file mode 100644 index 0000000..c07704c --- /dev/null +++ b/src/test/java/com/teketik/test/mockinbean/test/InterfaceImplementation2Test.java @@ -0,0 +1,36 @@ +package com.teketik.test.mockinbean.test; + + +import com.teketik.test.mockinbean.MockInBean; +import com.teketik.test.mockinbean.test.InterfaceImplementationTestConfig.LoggingService; +import com.teketik.test.mockinbean.test.InterfaceImplementationTestConfig.ProviderService; +import com.teketik.test.mockinbean.test.InterfaceImplementationTestConfig.ProviderServiceImpl; + +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; +import org.mockito.Mockito; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.annotation.Import; + +@Import(InterfaceImplementationTestConfig.class) +public class InterfaceImplementation2Test extends BaseTest { + + @Autowired + protected LoggingService loggingService; + + @MockInBean(LoggingService.class) + private ProviderService providerServiceMock; + + @MockInBean(LoggingService.class) + private ProviderServiceImpl providerServiceImpl; + + @Test + public void test() { + Mockito.when(providerServiceMock.provideValue()).thenReturn("mocked value"); + Mockito.when(providerServiceImpl.provideValue()).thenReturn("mocked value 2"); + + Assertions.assertEquals("mocked value", loggingService.logCurrentValue()); + Assertions.assertEquals("mocked value 2", loggingService.logCurrentValueWithImpl()); + } + +} diff --git a/src/test/java/com/teketik/test/mockinbean/test/InterfaceImplementationTestConfig.java b/src/test/java/com/teketik/test/mockinbean/test/InterfaceImplementationTestConfig.java new file mode 100644 index 0000000..514ce44 --- /dev/null +++ b/src/test/java/com/teketik/test/mockinbean/test/InterfaceImplementationTestConfig.java @@ -0,0 +1,39 @@ +package com.teketik.test.mockinbean.test; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; +import org.springframework.stereotype.Service; + +class InterfaceImplementationTestConfig { + + public interface ProviderService { + public String provideValue(); + } + + @Service + public static class ProviderServiceImpl implements ProviderService { + @Override + public String provideValue() { + return "value1"; + } + } + + @Component + public static class LoggingService { + + @Autowired + private ProviderService providerService; + + @Autowired + private ProviderServiceImpl providerServiceImpl; + + public String logCurrentValue() { + return providerService.provideValue(); + } + + public String logCurrentValueWithImpl() { + return providerServiceImpl.provideValue(); + } + } + +}