JpaUnitObjectFactory implements ObjectFactory { @Override public <T> T getInstance(final Class<T> clazz) { if (!definitions.containsKey(clazz)) { definitions.put(clazz, createProxy(clazz)); } return clazz.cast(definitions.get(clazz)); }  @Override void start(); @Override void stop(); @Override boolean addClass(final Class<?> glueClass); @Override T getInstance(final Class<T> clazz);  }
@Test public void testGetInstanceForSameTypeAlwaysReturnsSameObject() { final ClassA obj1 = factory.getInstance(ClassA.class); final ClassA obj2 = factory.getInstance(ClassA.class); assertNotNull(obj1); assertNotNull(obj2); assertThat(obj1, equalTo(obj2)); }
@Test public void testGetInstanceForSameTypeLeadsToExecutionOfProcessBeforeAllOnlyOnce() throws Exception { final ClassA obj1 = factory.getInstance(ClassA.class); final ClassA obj2 = factory.getInstance(ClassA.class); assertNotNull(obj1); assertNotNull(obj2); final ArgumentCaptor<TestInvocation> invocationCaptor = ArgumentCaptor.forClass(TestInvocation.class); verify(executor).processBeforeAll(invocationCaptor.capture()); final TestInvocation invocation = invocationCaptor.getValue(); assertThat(invocation.getTestClass(), equalTo(ClassA.class)); assertThat(invocation.getContext(), equalTo(ctx)); assertThat(invocation.getFeatureResolver(), notNullValue()); verifyNoMoreInteractions(executor); }
@Test public void testGetInstanceForDifferentTypesLeadsToExecutionOfProcessBeforeAllForEachType() throws Exception { final ClassA obj1 = factory.getInstance(ClassA.class); final ClassB obj2 = factory.getInstance(ClassB.class); assertNotNull(obj1); assertNotNull(obj2); final ArgumentCaptor<TestInvocation> invocationCaptor = ArgumentCaptor.forClass(TestInvocation.class); verify(executor, times(2)).processBeforeAll(invocationCaptor.capture()); final List<TestInvocation> invocations = invocationCaptor.getAllValues(); assertThat(invocations.get(0).getTestClass(), equalTo(ClassA.class)); assertThat(invocations.get(0).getContext(), equalTo(ctx)); assertThat(invocations.get(0).getFeatureResolver(), notNullValue()); assertThat(invocations.get(1).getTestClass(), equalTo(ClassB.class)); assertThat(invocations.get(1).getContext(), equalTo(ctx)); assertThat(invocations.get(1).getFeatureResolver(), notNullValue()); assertThat(invocations.get(0), not(equalTo(invocations.get(1)))); verifyNoMoreInteractions(executor); }
@Test public void testMethodsWithCucumberAnnotationsAreIntercepted() throws Exception { final ClassB obj = factory.getInstance(ClassB.class); obj.when(); verify(executor).processBeforeAll(notNull(TestInvocation.class)); final InOrder inOrder = inOrder(executor); inOrder.verify(executor).processBefore(notNull(TestInvocation.class)); inOrder.verify(executor).processAfter(notNull(TestInvocation.class)); verifyNoMoreInteractions(executor); }
@Test public void testMethodsWithoutCucumberAnnotationsAreNotIntercepted() throws Exception { final ClassB obj = factory.getInstance(ClassB.class); obj.notACucumberMethod(); verify(executor).processBeforeAll(notNull(TestInvocation.class)); verifyNoMoreInteractions(executor); }