Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix mock implementations of interface #32

Merged
merged 2 commits into from
Nov 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/main/java/com/teketik/test/mockinbean/BeanUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
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());
}

}
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());
}

}
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();
}
}

}