Field implements IField { @Override public <T> T in(final IObject obj) throws ReadValueException, InvalidArgumentException { if (null == obj) { throw new InvalidArgumentException("Argument should not be null."); } Object value = obj.getValue(fieldName); if (null == value) { return null; } return (T) value; } Field(final IFieldName fieldName); @Override T in(final IObject obj); @Override T in(final IObject obj, final Class type); @Override void out(final IObject obj, final T in);  }
@Test public void checkInMethod() throws Exception { IField field = new Field(new FieldName("a")); IObject env = mock(IObject.class); when(env.getValue(new FieldName("a"))).thenReturn(true); assertTrue(field.in(env)); }
@Test (expected = InvalidArgumentException.class) public void checkInMethodExceptionOnNullArgument() throws Exception { IField field = new Field(new FieldName("a")); field.in(null); fail(); }
@Test public void checkInMethodWithTypeCast() throws Exception { IResolveDependencyStrategy strategy = mock(IResolveDependencyStrategy.class); when(strategy.resolve(1)).thenReturn("1"); IOC.register( IOC.resolve(IOC.getKeyForKeyStorage(), String.class.getCanonicalName() + "convert"), strategy ); IField field = new Field(new FieldName("a")); IObject env = mock(IObject.class); when(env.getValue(new FieldName("a"))).thenReturn(1); String result = field.in(env, String.class); assertEquals(result, "1"); }
@Test public void checkInMethodWithTypeCastOnEqualRequiredTypeWithExisting() throws Exception { IField field = new Field(new FieldName("a")); IObject env = mock(IObject.class); when(env.getValue(new FieldName("a"))).thenReturn("1"); String result = field.in(env, String.class); assertEquals(result, "1"); }
@Test (expected = InvalidArgumentException.class) public void checkInMethodWithTypeCastExceptionOnAbsentStrategy() throws Exception { IField field = new Field(new FieldName("a")); IObject env = mock(IObject.class); when(env.getValue(new FieldName("a"))).thenReturn(1); field.in(env, String.class); fail(); }
@Test (expected = InvalidArgumentException.class) public void checkInMethodWithTypeCastExceptionOnNullFirstArgument() throws Exception { IField field = new Field(new FieldName("a")); field.in(null, String.class); fail(); }
@Test (expected = InvalidArgumentException.class) public void checkInMethodWithTypeCastExceptionOnNullSecondArgument() throws Exception { IObject env = mock(IObject.class); IField field = new Field(new FieldName("a")); field.in(env, null); fail(); }