GSON default serializer failed in JUnit - junit4

I am using Gson to serialize an object, it works fine in the execution. But when I am adding JUnit4 testing case using the latest PowerMockito. I am seeing error of
"UnsuppoprtedOperationException: Attempted to serialize java.lang.Class: com.mmypackage.MyClass. Forgot to register a type adapter?"
Why is it different in JUnit4/PowerMockito and normal execution?

Related

UserEntity$Description and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS)

I am developing the Spring Boot + MongoDB example and I am getting below error.
I used Lombok to generate setters and getters. I am simply calling the repositoy.findAll() method and giving me the below error.
Whitelabel Error PageThis application has no explicit mapping for /error, so you are seeing this as a fallback.Sat Feb 10 16:49:04 IST 2018There was an unexpected error (type=Internal Server Error, status=500).Could not write JSON: No serializer found for class com.myfi.model.UserEntity$Description and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS); nested exception is com.fasterxml.jackson.databind.JsonMappingException: No serializer found for class com.myfi.model.UserEntity$Description and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS) (through reference chain: java.util.ArrayList[0]->com.myfi.model.Tweet["user"]->com.myfi.model.User["entities"]->com.myfi.model.UserEntity["description"])
I was seeing the same issue in my project.
For me, my problem was I was running the application within eclipse, and eclipse was not aware of the lombok annotations. When I added the lombok plugin to eclipse it started working

Playframework [Scala]: Class not found in classpath in Play for Scala

I have added to build.sbt the Apache Derby jar:
libraryDependencies += "org.apache.derby" % "derby" % "10.14.1.0" % Test
I also ran activator update, which seemed to work fine.
I need to embed Derby's database within the application, so I'm trying to instantiate the connection to it:
println(1)
Class.forName("org.apache.derby.jdbc.EmbeddedDriver").newInstance()
println(2)
val url = "jdbc:derby://localhost:1527/tempdb;create=true;user=me;password=mine"
val conn = DriverManager.getConnection(url)
println(3)
The above code only prints 1 and then generates an exception. I think it is mostly because the class is not in the classpath (the exception message is just the class name). What is missing here?
Firstly, there is no need to invoke .newInstance() on the driver class object. The "classic" way to do is just to call Class.forName(arg) in order to force the class to be loaded.
Secondly, Class.forName(arg) call is redundant and not necessary since JBDC 4, as DriverManager doc says:
Applications no longer need to explictly load JDBC drivers using Class.forName(). Existing programs which currently load JDBC drivers using Class.forName() will continue to work without modification.
Thirdly, to benefit from the standard way to configure DB connection in Play! Framework, it could be useful to try it through configuration in application.conf file:
db.default.driver=org.apache.derby.jdbc.EmbeddedDriver
db.default.url="jdbc:derby://localhost:1527/tempdb..."
More details here.
Fourthly, % Test scope in the dependency declaration looks suspicious. If the connection is to be used in the main code of the app, the dependency will be ignored and the class will not be found.

New dependency causing java.lang.NoSuchMethodError exception

I'm working on a project that is making use of a Scala ETCD client (https://github.com/nikore/scala-etcd), this has been all fine and good until I added a Scala Fleet client (https://github.com/MonsantoCo/fleet-client) into the mix.
When I compile it is fine but once I run the project it throws the following exception on the lines where the ETCDClient is constructed
Exception in thread "main" java.lang.NoSuchMethodError: spray.json.StandardFormats$class.optionFormat(Lspray/json/StandardFormats;Lspray/json/JsonFormat;)Lspray/json/StandardFormats$OptionFormat;
at net.nikore.etcd.EtcdJsonProtocol$.optionFormat(EtcdJsonProtocol.scala:5)
at net.nikore.etcd.EtcdJsonProtocol$.<init>(EtcdJsonProtocol.scala:18)
at net.nikore.etcd.EtcdJsonProtocol$.<clinit>(EtcdJsonProtocol.scala)
at net.nikore.etcd.EtcdClient.<init>(EtcdClient.scala:76)
at net.nikore.etcd.EtcdClient$.apply(EtcdClient.scala:20)
at au.com.someproject.helpers.SomeProjectService$class.main(SomeProjectService.scala:116)
at au.com.someproject.some_agent.ServiceMain$.main(ServiceMain.scala:17)
at au.com.someproject.some_agent.ServiceMain.main(ServiceMain.scala)
The only thing that I call tell that might be causing this is that the ETCD Client requires spray-json 1.3.1 but the Fleet Client requires spray-json 1.3.2. Having a look at the compiled package, spray-json 1.3.2 is packaged.
What is the cause of this (if I am wrong about the dependencies) and how can I go about resolving this?
EDIT:
Bellow is the code at SomeProjectService.scala:116:
etcdClient = EtcdClient(s"http://${serviceConfiguration.getString("someproject.etcd.host")}:${serviceConfiguration.getString("someproject.etcd.port")}")
And bellow is the code at ServiceMain.scala:17:
object ServiceMain extends SomeProjectService {
ServiceMain is the entry point for the service but the main function is declared in the SomeProjectService trait where the etcdClient is declared

NoSuchMethod exception in Flink when using dataset with custom object array

I have a problem with Flink
java.lang.NoSuchMethodError: org.apache.flink.api.java.typeutils.ObjectArrayTypeInfo.getInfoFor(Lorg/apache/flink/api/common/typeinfo/TypeInformation;)Lorg/apache/flink/api/java/typeutils/ObjectArrayTypeInfo;
at LowLevel.FlinkImplementation.FlinkImplementation$$anon$6.<init>(FlinkImplementation.scala:28)
at LowLevel.FlinkImplementation.FlinkImplementation.<init>(FlinkImplementation.scala:28)
at IRLogic.GmqlServer.<init>(GmqlServer.scala:15)
at it.polimi.App$.main(App.scala:20)
at it.polimi.App.main(App.scala)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
...
the line with the problem is this one
implicit val regionTypeInformation =
api.scala.createTypeInformation[FlinkDataTypes.FlinkRegionType]
in the FlinkRegionType I have an Array of custom object
I developed the app with the maven plugin in the IDE and everything is working good, but when I move to the version I downloaded from the website I get the error above
I am using Flink 0.9
I was thinking that some library may be missing but I am using maven for handling everything. Moreover running through the code of ObjectArrayTypeInfo.java it doesn't seem to be the problem
A NoSuchMethodError commonly indicates a version mismatch between the libraries a Flink program was compiled with and the system the program is executed on. Especially if the same code works in an IDE setup where compile and execution libraries are the same.
In such case, you should check the version of the Flink dependencies, for example in the Maven POM file.

NoClassDefFoundError: in Google App Engine app while creating Gson instance

Is there something tricky about using Gson in Google App Engine App? I placed gson jar in war/lib and included it in the build path. I'm using Eclipse.
Code compiles however app keeps failing at line:
Gson gson = new Gson();
with:
java.lang.NoClassDefFoundError: com/google/gson/Gson
A NoClassDefFoundError specifies that you have a particular class available at compile time but missing or not initialized during runtime. In your case com/google/gson/Gson class is found at compile time as gson jar is in your lib. But during runtime it is either not present or not initialized. Check for a conflict of gson jar or if there is an ExceptionInInitializerError in the stacktrace. You may further understand it here - Debugging a NoClassDefFoundError
I know GAE by name only, but I have that kind of error in Eclipse when I have a dynamic web project and I forget to deploy libs I used to build.
So, maybe you can solve by going into Deployment Assembly section of your project and add libs that you have in your build path.
The problem was that I placed library under war/libs and not under war/WEB-INF/lib