Created
July 15, 2015 18:23
-
-
Save jnericks/d2d0125b7ce8b7be4262 to your computer and use it in GitHub Desktop.
Revisions
-
jnericks created this gist
Jul 15, 2015 .There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,194 @@ package com.jnericks.testutils; import org.junit.*; import java.util.function.*; import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.StrictAssertions.assertThatThrownBy; import static org.mockito.Mockito.*; public class SystemUnderTestFactoryTester extends BaseUnitTester { SystemUnderTestFactory<System> sutFactory; @Before public void before() { sutFactory = new SystemUnderTestFactory<System>() { }; } @Test public void should_be_able_to_create_sut() { System sut = sutFactory.sut(); assertThat(sut).isExactlyInstanceOf(System.class); } @Test public void should_have_sut_be_a_singleton() { System sut1 = sutFactory.sut(); System sut2 = sutFactory.sut(); assertThat(sut1).isSameAs(sut2); } @Test public void should_have_dependency_a() { DependencyA a = sutFactory.dependency(DependencyA.class); assertThat(a).isInstanceOf(DependencyA.class); } @Test public void should_have_dependency_b() { DependencyB a = sutFactory.dependency(DependencyB.class); assertThat(a).isInstanceOf(DependencyB.class); } @Test public void should_be_able_to_do_a_stuff() { sutFactory.sut().doAStuff(); verify(sutFactory.dependency(DependencyA.class), Once).aStuff(); } @Test public void should_be_able_to_do_b_stuff() { sutFactory.sut().doBStuff(); verify(sutFactory.dependency(DependencyB.class), Once).bStuff(); } @Test public void should_be_able_to_stub_a_method() { Object objectPassedToSut = new Object(); Object objectReturnedFromDependencyA = new Object(); when(sutFactory.dependency(DependencyA.class).doSomething(objectPassedToSut)) .thenReturn(objectReturnedFromDependencyA); Object actual = sutFactory.sut().passToDependencyA(objectPassedToSut); assertThat(actual).isSameAs(objectReturnedFromDependencyA); } @Test public void should_be_able_to_retrieve_injected_substitute() { DependencyA myDependencyA = mock(DependencyA.class); sutFactory.forDependency(DependencyA.class).use(myDependencyA); assertThat(sutFactory.dependency(DependencyA.class)).isSameAs(myDependencyA); } @Test public void should_be_able_to_assert_on_injected_substitute() { DependencyA myDependencyA = mock(DependencyA.class); sutFactory.forDependency(DependencyA.class).use(myDependencyA); sutFactory.sut().doAStuff(); verify(myDependencyA, Once).aStuff(); } @Test public void should_throw_exception_when_configuring_an_object_that_is_NOT_a_dependency() { assertThatThrownBy(() -> sutFactory.forDependency(NotADependency.class).use(mock(NotADependency.class))) .isInstanceOf(UnsupportedOperationException.class); } @Test public void should_throw_exception_when_retrieving_object_that_is_NOT_a_dependency() { assertThatThrownBy(() -> sutFactory.dependency(NotADependency.class)) .isInstanceOf(UnsupportedOperationException.class); } @Test public void should_throw_exception_when_trying_to_retrieve_sut_from_method() { assertThatThrownBy(() -> sutFactory.dependency(System.class)) .isInstanceOf(UnsupportedOperationException.class); } @Test public void should_be_able_to_override_internal_sut_factory() { System system = mock(System.class); sutFactory.createSutUsing(() -> system); assertThat(sutFactory.sut()).isSameAs(system); } @Test @SuppressWarnings("unchecked") public void should_be_able_to_add_a_pre_processor() { Runnable runnable = mock(Runnable.class); sutFactory.beforeSutCreated(runnable); sutFactory.createSut(); verify(runnable, Once).run(); } @Test @SuppressWarnings("unchecked") public void should_be_able_to_add_a_post_processor() { Consumer<System> consumer = mock(Consumer.class); sutFactory.afterSutCreated(consumer); sutFactory.createSut(); verify(consumer, Once).accept(sutFactory.sut()); } @Test @SuppressWarnings("unchecked") public void should_be_able_to_add_pre_and_post_processors_to_custom_sut_creation() { Runnable runnable = mock(Runnable.class); Consumer<System> consumer = mock(Consumer.class); sutFactory.beforeSutCreated(runnable); sutFactory.afterSutCreated(consumer); sutFactory.createSutUsing(() -> new System(null)); sutFactory.createSut(); verify(runnable, Once).run(); verify(consumer, Once).accept(sutFactory.sut()); } @Test @SuppressWarnings("unchecked") public void should_be_able_to_swap_in_a_concrete_impl_of_a_dependency() { Object objectPassedIn = new Object(); Object objectReturned = new Object(); Runnable runnable = mock(Runnable.class); Function<Object, Object> function = mock(Function.class); when(function.apply(objectPassedIn)).thenReturn(objectReturned); DependencyA impl = new DependencyAImpl(runnable, function); sutFactory.forDependency(DependencyA.class).use(impl); sutFactory.sut().doAStuff(); verify(runnable, Once).run(); Object actual = sutFactory.sut().passToDependencyA(objectPassedIn); verify(function, Once).apply(objectPassedIn); assertThat(actual).isSameAs(objectReturned); } }