StringToIObjectResolveDependencyStrategy implements IResolveDependencyStrategy { @Override public <T> T resolve(final Object... args) throws ResolveDependencyStrategyException { try { String jsonBody = String.valueOf(args[0]); return (T) new DSObject(jsonBody); } catch (Exception e) { throw new ResolveDependencyStrategyException("Can't create IObject from String.", e); } }  @Override T resolve(final Object... args);  }
@Test public void ShouldCorrectConvertStringToIObject() throws Exception { String json = "{" + "\"key1\": 123," + "\"key2\": \"value\"" + "}"; IObject result = strategy.resolve(json); assertEquals(result.getValue(new FieldName("key1")), 123); assertEquals(result.getValue(new FieldName("key2")), "value"); }
@Test(expected = ResolveDependencyStrategyException.class) public void ShouldThrowException_When_StringIsIncorrect() throws Exception { String invalid = "invalid"; strategy.resolve(invalid); fail(); }
@Test(expected = ResolveDependencyStrategyException.class) public void ShouldThrowException_When_AnyErrorIsOccurred() throws Exception { mockStatic(String.class); when(String.valueOf(anyString())).thenThrow(new RuntimeException("")); strategy.resolve(""); fail(); }
@Test(expected = ResolveDependencyStrategyException.class) public void ShouldThrowException_When_NullIsPassed() throws Exception { strategy.resolve(null); fail(); }