You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I'm not able to find an issue similar to this here or anywhere else. So, I can't tell if it's a bug or not. However, I have a very simple test case that is failing and I'm not able to find the root cause. I'd appreciate if you can help.
Issue description
I have a Java class that will parse a given JSON string. The method that parses the string will throw NullPointerException if expected JSON entry is not found. I added a Groovy Spock test to validate this.
The test case is failing with following error message:
Expected exception of type 'java.lang.NullPointerException', but no exception was thrown
at org.spockframework.lang.SpecInternals.checkExceptionThrown(SpecInternals.java:85)
at org.spockframework.lang.SpecInternals.thrownImpl(SpecInternals.java:72)
However, if I run the test as gradlew test --info, I see that the NullPointerException in the STANDARD_ERROR
com.amimas.MyClassSpec > NullPointerException will be thrown if expected json entities are not found STANDARD_ERROR
java.lang.NullPointerException
at com.amimas.RequestProcessor.validateRequest(RequestHandler.java:79)
at com.amimas.RequestProcessor$validateRequest$0.call(Unknown Source)
at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:48)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:113)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:117)
How to reproduce
Have a Java Class
Have a method in the class that throws exception
Add a spock test case to validate that expected exception is thrown
def "NullPointerException will be thrown if expected json entities are not found" () {
given:
String request = '{"user":"john","location":"CA"}'
RequestProcessor rp = new RequestProcessor()
rp.setRequest(request)
when: "a new instance of RequestHandler receives an empty request"
rp.validateRequest()
then: "an Exception being thrown"
thrown(NullPointerException)
Additional Environment information
Version of your build tool(if used), Java, Groovy, IDE, OS etc
Is this your real code of the RequestProcessor? Then that's no issue with Spock.
First you never throw an NullPointerException in your code.
Second you catch all exceptions to log them. But you do not rethrow them. So the method will simply never fail.
Third your method accidentally creates a NPE - because it access a null-object somewhere. So you see a NPE as stacktrace.
Fourth the code simply does not reflect what you comment in your spec.
I'm not able to find an issue similar to this here or anywhere else. So, I can't tell if it's a bug or not. However, I have a very simple test case that is failing and I'm not able to find the root cause. I'd appreciate if you can help.
Issue description
I have a Java class that will parse a given JSON string. The method that parses the string will throw
NullPointerException
if expected JSON entry is not found. I added a Groovy Spock test to validate this.The test case is failing with following error message:
However, if I run the test as
gradlew test --info
, I see that theNullPointerException
in the STANDARD_ERRORHow to reproduce
Link to a gist or similar (optional)
build.gradle
:RequestProcessor.java
RequestProcessorSpec.groovy
Additional Environment information
Java/JDK
JDK 1.8
Groovy version
Groovy 2.4
Build tool version
Gradle
Gradle 3.5
Operating System
Mac
IDE
n/a
Build-tool dependencies used
Gradle/Grails
The text was updated successfully, but these errors were encountered: