Skip to content

Instantly share code, notes, and snippets.

Embed
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;
@RunWith(MockitoJUnitRunner.class)
public class DependencyOverrideExampleTests {
@Spy Component component = new Component(new DependencyA());
@Test
public void runWithDepenencyA() {
component.someLogic();
}
@Test
public void runWithDepenencyB() {
when(component.getDependency()).thenReturn(new DependencyB());
component.someLogic();
}
}
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.