public ToothPickRule(Object test, Object scopeName) { this.testModule = new ToothPickTestModule(test); if (scopeName != null) { setScopeName(scopeName); } }
@Override public Statement apply(Statement base, Description description) { return new ToothPickStatement(base); }
/** * Automatically binds all annotated mocks of a module. * * @param test the test whose {@code Mock} annotated fields will be mocked. */ public ToothPickTestModule(Object test) { bindAllMocks(test); }
/** * Bind all {@code Mock} annotated field of a given test. * * @param test the test whose fields are going to be injected. */ @SuppressWarnings("unchecked") public void bindAllMocks(Object test) { mockCount = 0; for (Field field : test.getClass().getDeclaredFields()) { Annotation mockAnnotation = findMockAnnotation(field); String injectionName = findInjectionName(field); if (mockAnnotation != null) { FieldValueProvider mockProvider = new FieldValueProvider(field, test); if (injectionName != null) { bind(field.getType()).withName(injectionName).toProviderInstance(mockProvider); } else { bind(field.getType()).toProviderInstance(mockProvider); } mockCount++; } } }
@Test(expected = RuntimeException.class) public void testGet_shouldFail_whenFieldIsNotAccessible() throws NoSuchFieldException { //GIVEN Foo foo = new Foo(); Field fieldS = Foo.class.getDeclaredField("s"); FieldValueProvider fieldValueProviderUnderTest = new FieldValueProvider(fieldS, foo); fieldS.setAccessible(false); //WHEN fieldValueProviderUnderTest.get(); //THEN fail("Should thrown an exception"); }
@Test public void testSetScopeName() throws Exception { toothPickRuleWithoutScopeName.setScopeName("Bar"); assertThat(toothPickRuleWithoutScopeName.getScope(), notNullValue()); assertThat(toothPickRuleWithoutScopeName.getScope().getName(), is((Object) "Bar")); assertThat(toothPickRuleWithoutScopeName.getTestModule(), notNullValue()); } }
@Test(expected = IllegalStateException.class) public void testSetScopeName_shouldFail_whenScopeNameWasAlreadySet() throws Exception { toothPickRule.setScopeName("Bar"); }
@Test public void testGetInstance_shouldReturnNamedBinding_whenAskingNamedBinding() throws Exception { //GIVEN ModuleWithNamedBindings moduleWithNamedBindings = new ModuleWithNamedBindings(); toothPickRule.getScope().installModules(moduleWithNamedBindings); //WHEN entryPoint = toothPickRule.getInstance(EntryPoint.class, "Foo"); //THEN assertThat(entryPoint, notNullValue()); assertThat(entryPoint, sameInstance(moduleWithNamedBindings.instance)); }
@Test public void testMock() throws Exception { //GIVEN expect(dependency.num()).andReturn(2); replay(dependency); //WHEN entryPoint = toothPickRule.getInstance(EntryPoint.class); int num = entryPoint.dependency.num(); //THEN verify(dependency); assertThat(entryPoint, notNullValue()); assertThat(entryPoint.dependency, notNullValue()); assertThat(num, is(2)); }
public <T> T getInstance(Class<T> clazz) { return getInstance(clazz, null); }
@Test public void testInject() throws Exception { //GIVEN EntryPoint entryPoint = new EntryPoint(); //WHEN toothPickRule.inject(entryPoint); //THEN assertThat(entryPoint, notNullValue()); assertThat(entryPoint.dependency, notNullValue()); }
@Test(expected = IllegalArgumentException.class) public void test() throws Exception { new ToothPickRule(this, "Foo").setRootRegistryPackage("foo"); } }
@Test public void testScopeNameSetByConstruction() throws Exception { assertThat(toothPickRule.getScope(), notNullValue()); assertThat(toothPickRule.getScope().getName(), is((Object) "Foo")); assertThat(toothPickRule.getTestModule(), notNullValue()); }
@Test public void testMultiply() throws Exception { //GIVEN expect(mockComputer.compute()).andReturn(4); expect(mockComputer2.compute()).andReturn(4); replay(mockComputer, mockComputer2); toothPickRule.inject(simpleEntryPointUnderTest); //WHEN int result = simpleEntryPointUnderTest.multiply(); //THEN assertThat(result, is(48)); verify(mockComputer); } }
@Override public Statement apply(Statement base, Description description) { wasRun = true; return super.apply(base, description); } };
public ModuleWithNamedBindings() { bind(EntryPoint.class).withName("Foo").toInstance(instance); } }
public ToothPickRule(Object test, Object scopeName) { this.testModule = new ToothPickTestModule(test); if (scopeName != null) { setScopeName(scopeName); } }
@Test public void testGet_shouldReturnFieldValue() throws NoSuchFieldException { //GIVEN Foo foo = new Foo(); Field fieldS = Foo.class.getDeclaredField("s"); FieldValueProvider fieldValueProviderUnderTest = new FieldValueProvider(fieldS, foo); //WHEN String s = (String) fieldValueProviderUnderTest.get(); //THEN assertThat(s, is(foo.s)); }
@Test(expected = IllegalStateException.class) public void testSetScopeName_shouldFail_whenScopeNameAlreadyContainsATestModule() throws Exception { toothPickRuleWithoutScopeName.setScopeName("Foo"); }
@Test public void testGetInstance() throws Exception { //GIVEN //WHEN entryPoint = toothPickRule.getInstance(EntryPoint.class); //THEN assertThat(entryPoint, notNullValue()); assertThat(entryPoint.dependency, notNullValue()); }