Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thomasdarimont/958ad18e597713d6f3fc to your computer and use it in GitHub Desktop.
Save thomasdarimont/958ad18e597713d6f3fc to your computer and use it in GitHub Desktop.
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