Skip to content

Instantly share code, notes, and snippets.

View CollectiveHealth-gists's full-sized avatar

CollectiveHealth-gists

View GitHub Profile
@CollectiveHealth-gists
CollectiveHealth-gists / ComputerTestUsingSetter.java
Created June 1, 2018 18:09
ComputerTest implemented using setters.
public class ComputerTestUsingSetter {
@Test
public void testPlayVideo() {
Computer computer = new Computer();
MotherBoard motherBoard = new MotherBoard();
SoundCard soundCard = new SoundCard();
VideoCard videoCard = new VideoCard();
SoundSystem soundSystem = Mockito.mock(SoundSystem.class);
DisplaySystem displaySystem = Mockito.mock(DisplaySystem.class);
@CollectiveHealth-gists
CollectiveHealth-gists / ComputerTestUsingTestModule.java
Created June 1, 2018 18:10
ComputerTest implemented using TestModule.
public class ComputerTestUsingTestModule {
@Test
public void testPlayVideo() {
Injector injector = Guice.createInjector(new TestModule()
.withMockedClasses(SoundSystem.class, DisplaySystem.class));
Computer computer = injector.getInstance(Computer.class);
SoundSystem soundSystem = injector.getInstance(SoundSystem.class);
DisplaySystem displaySystem = injector.getInstance(DisplaySystem.class);
public class ComputerTestModule extends TestModule {
@Override
protected Collection<ClassInstancePair<?>> getDefaultInstances() {
return Arrays.asList(
createClassMockPair(DisplaySystem.class),
createClassMockPair(SoundSystem.class));
}
}
@CollectiveHealth-gists
CollectiveHealth-gists / IntegrationWithDisplaySystemTest.java
Created June 1, 2018 18:12
Testing with real instance of DisplaySystem, but mocked out SoundSystem.
public class IntegrationWithDisplaySystemTest {
@Test
public void testDisplaySystem() {
Injector injector = Guice.createInjector(new ComputerTestModule()
.withInstance(DisplaySystem.class, new DisplaySystem()));
Computer computer = injector.getInstance(Computer.class);
// We are satisfied with no exception being thrown
@CollectiveHealth-gists
CollectiveHealth-gists / IntegrationWithSoundSystemTest.java
Created June 1, 2018 18:13
Testing with real instance of SoundSystem, but mocked out DisplaySystem.
public class IntegrationWithSoundSystemTest {
@Test
public void testSoundSystem() {
Injector injector = Guice.createInjector(new ComputerTestModule()
.withInstance(SoundSystem.class, new SoundSystem()));
Computer computer = injector.getInstance(Computer.class);
// We will be satisfied with no exception being thrown