Я пытаюсь покрыть служебный класс My JsonService с помощью Junits, но я получаю исключение, и я пытаюсь решить это долгое время, но проблема все еще не решена, может кто-нибудь мне помочь, пожалуйста.
JsonService
public final class JsonService {
private static final ObjectMapper MAPPER;
private JsonService() {
// Added to remove sonar issues.
}
static {
log.debug("Start of JsonService static block ");
MAPPER = new ObjectMapper();
MAPPER.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
MAPPER.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
MAPPER.setSerializationInclusion(Include.NON_NULL);
log.debug("End of JsonService static block ");
}
public static <T> T getObjectFromJson(final Object jsonString, final Class<T> valueType) {
// log.debug("Start of JsonService.getObjectFromJson() method.. {}");
T object = null;
if (jsonString != null) {
try {
object = MAPPER.readValue(jsonString.toString(), valueType);
// log.debug(DEBUG_LOG_STR, object);
} catch (IOException io) {
log.error(ERROR_LOG_STR + " in method getObjectFromJson(). Exception Message={}, Exception Stack ={}",
io.getMessage(), io);
throw new ServiceException(ErrorMessages.JSON_SERIALIZATION_ERROR, io.getCause());
}
}
// log.debug("End of JsonService.getObjectFromJson() method.. {}");
return object;
}
}
JsonServiceTest
public class JsonServiceTest {
@Test
public void getObjectFromJsonTest() throws Exception {
// PowerMockito.mockStatic(JsonService.class);
ObjectMapper mapper = new ObjectMapper();
TestHelper helper = new TestHelper();
helper.setName("name");
helper.setId(1);
String json = mapper.writeValueAsString(helper);
TestHelper actual = JsonService.getObjectFromJson(json, TestHelper.class);
assertEquals(actual.getName(), "name");
assertEquals(actual.getId(), 1);
}
class TestHelper {
String name;
long id;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public long getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
}
}
Исключение
com.att.idp.externalpartnerorder.exception.ServiceException
at com.att.idp.externalpartnerorder.common.util.JsonService.getObjectFromJson(JsonService.java:81)
at com.att.idp.externalpartnerorder.common.util.JsonServiceTest.getObjectFromJsonTest(JsonServiceTest.java:21)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:89)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:41)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:541)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:763)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:463)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:209)