Expand my Community achievements bar.

SOLVED

Gson object error during Junit using (powermock/mockito)

Avatar

Level 2
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?

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

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 

View solution in original post

2 Replies

Avatar

Correct answer by
Community Advisor

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 

Avatar

Community Advisor

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?