Ill be very specific about what i am trying to achieve here. Help would be greatly appreciated. I am currently creating a test JSON object like this:
String testTrainingRecord(int employeeId, String date, String courseTitle) {
String zeroDate = date.substring(0, 11) + "00:00:00";
return "{\"id\":0,\"employeeId\":" + employeeId + ",\"dateIdentified\":\"" + zeroDate + "\",\"dateFrom\":\"" + zeroDate + "\"," +
"\"dateTo\":\"" + zeroDate + "\",\"courseTitle\":\"" + courseTitle + "\",\"courseProvider\":\"APIprovider\",\"category\":\"API\",\"subject\":\"Recruitment\"," +
"\"location\":\"Bristol\",\"cost\":0,\"duration\":0,\"durationHours\":0,\"cpdhours\":0,\"cpdpoints\":0,\"evaluationCompleted\":true,\"nonAttendance\":true," +
"\"notes\":\"APInote\",\"reviewDate\":\"" + zeroDate + "\",\"developmentStatus\":\"APIdevelopment\",\"priorityLevel\":\"Level1\"," +
"\"lastUpdate\":\"" + zeroDate + "\"}";
}
I then do this when in my test which posts it to the API and this work fine:
.body(testTrainingRecord(employeeId, date, courseTitle)).log().all()
What i would like to do is something like this:
public class testTrainingRecord2 {
HashMap<String, String> hashMapString = new HashMap<String, String>();
HashMap<String, Integer> hashMapInt = new HashMap<String, Integer>();
HashMap<String, Boolean> hashMapIBool = new HashMap<String, Boolean>();
public testTrainingRecord2(int employeeId, String date, String courseTitle) {
String zeroDate = date.substring(0, 11) + "00:00:00";
hashMapInt.put("id", 0);
hashMapInt.put("employeeId", employeeId);
hashMapString.put("dateIdentified", zeroDate);
hashMapString.put("dateFrom", zeroDate);
hashMapString.put("dateTo", zeroDate);
hashMapString.put("courseTitle", courseTitle);
hashMapString.put("courseProvider", "APIprovider");
hashMapString.put("category", "API");
hashMapString.put("subject", "Recruitment");
hashMapString.put("location", "Bristol");
hashMapInt.put("cost", 0);
hashMapInt.put("duration", 0);
hashMapInt.put("durationHours", 0);
hashMapInt.put("cpdhours", 0);
hashMapInt.put("cpdpoints", 0);
hashMapIBool.put("evaluationCompleted", true);
hashMapIBool.put("nonAttendance", true);
hashMapString.put("notes", "APInote");
hashMapString.put("reviewDate", zeroDate);
hashMapString.put("developmentStatus", "APIdevelopment");
hashMapString.put("priorityLevel", "Level1");
hashMapString.put("lastUpdate", zeroDate);
return;
}
}
Which i am attempting to post like this:
.body(new testTrainingRecord2(employeeId, date, courseTitle)).log().all()
However when the test runs it errors as follows:
com.fasterxml.jackson.databind.JsonMappingException: No serializer found for class TrainingTests$testTrainingRecord2 and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS) )
at com.fasterxml.jackson.databind.ser.impl.UnknownSerializer.failForEmpty(UnknownSerializer.java:59)
at com.fasterxml.jackson.databind.ser.impl.UnknownSerializer.serialize(UnknownSerializer.java:26)
at com.fasterxml.jackson.databind.ser.DefaultSerializerProvider.serializeValue(DefaultSerializerProvider.java:118)
at com.fasterxml.jackson.databind.ObjectMapper.writeValue(ObjectMapper.java:1819)
at com.fasterxml.jackson.databind.ObjectMapper$writeValue$0.call(Unknown Source)
at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:47)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:115)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:135)
at io.restassured.internal.mapping.Jackson2Mapper.serialize(Jackson2Mapper.groovy:53)
at io.restassured.internal.mapping.Jackson2Mapper.serialize(Jackson2Mapper.groovy)
at io.restassured.mapper.ObjectMapper$serialize.call(Unknown Source)
at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:47)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:115)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:127)
at io.restassured.internal.mapping.ObjectMapping.serializeWithJackson2(ObjectMapping.groovy:196)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:567)
at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:101)
at org.codehaus.groovy.runtime.callsite.StaticMetaMethodSite$StaticMetaMethodSiteNoUnwrapNoCoerce.invoke(StaticMetaMethodSite.java:149)
at org.codehaus.groovy.runtime.callsite.StaticMetaMethodSite.callStatic(StaticMetaMethodSite.java:100)
at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCallStatic(CallSiteArray.java:55)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callStatic(AbstractCallSite.java:196)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callStatic(AbstractCallSite.java:216)
at io.restassured.internal.mapping.ObjectMapping.serialize(ObjectMapping.groovy:141)
at io.restassured.internal.mapping.ObjectMapping$serialize.call(Unknown Source)
at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:47)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:115)
at io.restassured.internal.RequestSpecificationImpl.body(RequestSpecificationImpl.groovy:750)
at TrainingTests.testPostTrainingRecord(TrainingTests.java:105)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:567)
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.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
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.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
at com.intellij.rt.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:33)
at com.intellij.rt.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:230)
at com.intellij.rt.junit.JUnitStarter.main(JUnitStarter.java:58)
Would anybody be kind enough to help please? :)
testTrainingRecord2doesn't have any public accessible property (stackoverflow.com/questions/8367312/…) and even if you provide getters and setters for the hashmaps, the object wont reflect the json at all. The properties that you put in the hashmaps should be public fields of the class.