-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix mock implementations of interface (#32)
- Loading branch information
1 parent
20fbc5d
commit ad342ad
Showing
4 changed files
with
111 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
src/test/java/com/teketik/test/mockinbean/test/InterfaceImplementation1Test.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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()); | ||
} | ||
|
||
} |
36 changes: 36 additions & 0 deletions
36
src/test/java/com/teketik/test/mockinbean/test/InterfaceImplementation2Test.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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()); | ||
} | ||
|
||
} |
39 changes: 39 additions & 0 deletions
39
src/test/java/com/teketik/test/mockinbean/test/InterfaceImplementationTestConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(); | ||
} | ||
} | ||
|
||
} |