Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
import static org.mockito.Mockito.*;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Spy;
import org.mockito.runners.MockitoJUnitRunner;
public class DependencyOverrideExampleTests {
@Spy Component component = new Component(new DependencyA());
public void runWithDepenencyA() {
public void runWithDepenencyB() {
when(component.getDependency()).thenReturn(new DependencyB());
interface Dependency {}
class DependencyA implements Dependency {}
class DependencyB implements Dependency {}
class Component {
private final Dependency dependency;
public Component(Dependency dependency) {
this.dependency = dependency;
public Dependency getDependency() {
return dependency;
public void someLogic() {
System.out.println("using dependency: " + getDependency());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.