Created
June 23, 2014 11:18
-
-
Save thomasdarimont/958ad18e597713d6f3fc to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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