If you're trying to inject different implementations of the same interface and then run parameterized tests on these implementations, you should manage the instantiation and injection of these implementations yourself. Below I'll show you how to structure your test class to achieve this:
-
Create a factory method to provide different implementations: Use @MethodSource to provide different implementations as parameters.
-
Ensure mocks are correctly injected: Use a test setup method to inject mocks into the implementations.
Here’s a sample implementation:
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.extension.ExtendWith;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.MethodSource;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.mockito.junit.jupiter.MockitoExtension;
import java.util.stream.Stream;
import static org.mockito.Mockito.*;
import static org.junit.jupiter.api.Assertions.*;
@ExtendWith(MockitoExtension.class) // Enable Mockito support
public class MyServiceTests {
// Mock dependencies
@Mock
private DependencyService mockDependencyService;
@BeforeEach
public void setUp() {
// Manually initialize mocks
MockitoAnnotations.openMocks(this);
}
// Provide different implementations of the interface
private static Stream<MyInterface> provideImplementations() {
return Stream.of(
new MyServiceImpl1(),
new MyServiceImpl2()
);
}
@ParameterizedTest
@MethodSource("provideImplementations")
public void testMyService(MyInterface service) {
// Use the injected mock in your test
if (service instanceof MyServiceImpl1) {
((MyServiceImpl1) service).setDependencyService(mockDependencyService);
} else if (service instanceof MyServiceImpl2) {
((MyServiceImpl2) service).setDependencyService(mockDependencyService);
}
when(mockDependencyService.someMethod()).thenReturn("mockedValue");
// Call a method on service and perform assertions
String result = service.process("input");
// Example assertions
assertNotNull(result);
verify(mockDependencyService).someMethod();
}
}
Key Points:
-
Mock Dependencies: Declare mock dependencies such as DependencyService.
-
Setup Method: Use a @BeforeEach method to initialize mocks manually using MockitoAnnotations.openMocks(this).
-
Provide Implementations: Create a static method provideImplementations annotated with @MethodSource, returning different concrete implementations of MyInterface.
-
Dependency Injection in Test: In your parameterized test method, inject the mock into the service implementations by checking their types and setting the mock dependency.
Here's an example of how your MyServiceImpl1 and MyServiceImpl2 might look:
public class MyServiceImpl1 implements MyInterface {
private DependencyService dependencyService;
// Setter for injection
public void setDependencyService(DependencyService dependencyService) {
this.dependencyService = dependencyService;
}
@Override
public String process(String input) {
return dependencyService.someMethod();
}
}
public class MyServiceImpl2 implements MyInterface {
private DependencyService dependencyService;
// Setter for injection
public void setDependencyService(DependencyService dependencyService) {
this.dependencyService = dependencyService;
}
@Override
public String process(String input) {
return dependencyService.someMethod();
}
}
In this setup, MyServiceImpl1 and MyServiceImpl2 both implement MyInterface and have a setter method for the dependency injection.
This approach ensures that you're able to leverage parameterized testing with different service implementations while also ensuring that those implementations are properly configured with the necessary mocked dependencies.