@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)); }
/** * 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++; } } }
/** * 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"); }
/** * 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).toProvider(mockProvider); } else { bind(field.getType()).toProvider(mockProvider); } mockCount++; } } }