Gson object error during Junit using (powermock/mockito) | Community
Skip to main content
Level 2
November 1, 2022
Solved

Gson object error during Junit using (powermock/mockito)

  • November 1, 2022
  • 2 replies
  • 4266 views
LocalPageResponse localPageResponse = createLocalPageService.createLocalPage(filePath);
Gson finalResponse = new Gson();
response.getWriter().print(finalResponse.toJson(localPageResponse));

List<String> outputLogs = new ArrayList<>();

this is piece of code from servlet , while writing test class for same , not able to pass this "Gson finalResponse = new Gson()" line od code and showing error like

 

java.lang.NoClassDefFoundError: com/google/gson/internal/bind/JsonTreeReader

at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:264)
at javassist.runtime.Desc.getClassObject(Desc.java:43)
at javassist.runtime.Desc.getClassType(Desc.java:152)
at javassist.runtime.Desc.getType(Desc.java:122)
at javassist.runtime.Desc.getType(Desc.java:78)

......

I am trying to execute the test class, has anyone faced the same issue and if yes, how was it resolved?

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by Lokesh_Vajrala

The version of Gson dependency - com.google.gson in code may need to be updated or add the dependency if missing with the test scope. The error says that the JsonTreeReader class is not available in the classpath to execute the test.

 

There is this thread clearly explains on the NoClassDefFoundError - https://stackoverflow.com/questions/17973970/how-can-i-solve-java-lang-noclassdeffounderror 

2 replies

Lokesh_Vajrala
Community Advisor
Lokesh_VajralaCommunity AdvisorAccepted solution
Community Advisor
November 1, 2022

The version of Gson dependency - com.google.gson in code may need to be updated or add the dependency if missing with the test scope. The error says that the JsonTreeReader class is not available in the classpath to execute the test.

 

There is this thread clearly explains on the NoClassDefFoundError - https://stackoverflow.com/questions/17973970/how-can-i-solve-java-lang-noclassdeffounderror 

Saravanan_Dharmaraj
Community Advisor
Community Advisor
November 1, 2022

What is  gson dependency version in your parent pom? 

 

<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.5</version>
<scope>provided</scope>
</dependency>

Try to update the gson to this or higher and see if its working?