Add proper unit tests for dependency-injection pattern

This commit is contained in:
Jeroen Meulemeester
2015-12-10 21:18:16 +01:00
committed by Jeroen Meulemeester
parent 0643289c74
commit 29fc56002a
5 changed files with 209 additions and 0 deletions

View File

@ -0,0 +1,40 @@
package com.iluwatar.dependency.injection;
import org.junit.Test;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoMoreInteractions;
/**
* Date: 12/10/15 - 8:40 PM
*
* @author Jeroen Meulemeester
*/
public class AdvancedWizardTest extends StdOutTest {
/**
* Test if the {@link AdvancedWizard} smokes whatever instance of {@link Tobacco} is passed to him
* through the constructor parameter
*/
@Test
public void testSmokeEveryThing() throws Exception {
final Tobacco[] tobaccos = {
new OldTobyTobacco(), new RivendellTobacco(), new SecondBreakfastTobacco()
};
for (final Tobacco tobacco : tobaccos) {
final AdvancedWizard advancedWizard = new AdvancedWizard(tobacco);
advancedWizard.smoke();
// Verify if the wizard is smoking the correct tobacco ...
verify(getStdOutMock(), times(1)).println("AdvancedWizard smoking " + tobacco.getClass().getSimpleName());
// ... and nothing else is happening.
verifyNoMoreInteractions(getStdOutMock());
}
}
}

View File

@ -0,0 +1,78 @@
package com.iluwatar.dependency.injection;
import com.google.inject.AbstractModule;
import com.google.inject.Guice;
import com.google.inject.Injector;
import org.junit.Test;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoMoreInteractions;
/**
* Date: 12/10/15 - 8:57 PM
*
* @author Jeroen Meulemeester
*/
public class GuiceWizardTest extends StdOutTest {
/**
* Test if the {@link GuiceWizard} smokes whatever instance of {@link Tobacco} is passed to him
* through the constructor parameter
*/
@Test
public void testSmokeEveryThingThroughConstructor() throws Exception {
final Tobacco[] tobaccos = {
new OldTobyTobacco(), new RivendellTobacco(), new SecondBreakfastTobacco()
};
for (final Tobacco tobacco : tobaccos) {
final GuiceWizard guiceWizard = new GuiceWizard(tobacco);
guiceWizard.smoke();
// Verify if the wizard is smoking the correct tobacco ...
verify(getStdOutMock(), times(1)).println("GuiceWizard smoking " + tobacco.getClass().getSimpleName());
// ... and nothing else is happening.
verifyNoMoreInteractions(getStdOutMock());
}
}
/**
* Test if the {@link GuiceWizard} smokes whatever instance of {@link Tobacco} is passed to him
* through the Guice google inject framework
*/
@Test
public void testSmokeEveryThingThroughInjectionFramework() throws Exception {
@SuppressWarnings("unchecked")
final Class<? extends Tobacco>[] tobaccos = new Class[]{
OldTobyTobacco.class, RivendellTobacco.class, SecondBreakfastTobacco.class
};
for (final Class<? extends Tobacco> tobaccoClass : tobaccos) {
// Configure the tobacco in the injection framework ...
final Injector injector = Guice.createInjector(new AbstractModule() {
@Override
protected void configure() {
bind(Tobacco.class).to(tobaccoClass);
}
});
// ... and create a new wizard with it
final GuiceWizard guiceWizard = injector.getInstance(GuiceWizard.class);
guiceWizard.smoke();
// Verify if the wizard is smoking the correct tobacco ...
verify(getStdOutMock(), times(1)).println("GuiceWizard smoking " + tobaccoClass.getSimpleName());
// ... and nothing else is happening.
verifyNoMoreInteractions(getStdOutMock());
}
}
}

View File

@ -0,0 +1,33 @@
package com.iluwatar.dependency.injection;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import java.io.PrintStream;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoMoreInteractions;
/**
* Date: 12/10/15 - 8:26 PM
*
* @author Jeroen Meulemeester
*/
public class SimpleWizardTest extends StdOutTest {
/**
* Test if the {@link SimpleWizard} does the only thing it can do: Smoke it's {@link
* OldTobyTobacco}
*/
@Test
public void testSmoke() {
final SimpleWizard simpleWizard = new SimpleWizard();
simpleWizard.smoke();
verify(getStdOutMock(), times(1)).println("SimpleWizard smoking OldTobyTobacco");
verifyNoMoreInteractions(getStdOutMock());
}
}

View File

@ -0,0 +1,53 @@
package com.iluwatar.dependency.injection;
import org.junit.After;
import org.junit.Before;
import java.io.PrintStream;
import static org.mockito.Mockito.mock;
/**
* Date: 12/10/15 - 8:37 PM
*
* @author Jeroen Meulemeester
*/
public abstract class StdOutTest {
/**
* The mocked standard out {@link PrintStream}, required since the actions of the wizard don't
* have any influence on any other accessible objects, except for writing to std-out using {@link
* System#out}
*/
private final PrintStream stdOutMock = mock(PrintStream.class);
/**
* Keep the original std-out so it can be restored after the test
*/
private final PrintStream stdOutOrig = System.out;
/**
* Inject the mocked std-out {@link PrintStream} into the {@link System} class before each test
*/
@Before
public void setUp() {
System.setOut(this.stdOutMock);
}
/**
* Removed the mocked std-out {@link PrintStream} again from the {@link System} class
*/
@After
public void tearDown() {
System.setOut(this.stdOutOrig);
}
/**
* Get the mocked stdOut {@link PrintStream}
*
* @return The stdOut print stream mock, renewed before each test
*/
final PrintStream getStdOutMock() {
return this.stdOutMock;
}
}