Skip to content

Commit

Permalink
Fix mock implementations of interface (#32)
Browse files Browse the repository at this point in the history
  • Loading branch information
antoinemeyer authored Nov 30, 2024
1 parent 20fbc5d commit ad342ad
Show file tree
Hide file tree
Showing 4 changed files with 111 additions and 1 deletion.
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();
}
}

}

0 comments on commit ad342ad

Please sign in to comment.