Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expected exception but no exception was thrown but standard error shows expected exception #827

Closed
amimas opened this issue Mar 8, 2018 · 2 comments

Comments

@amimas
Copy link

amimas commented Mar 8, 2018

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

  1. Have a Java Class
  2. Have a method in the class that throws exception
  3. Add a spock test case to validate that expected exception is thrown

Link to a gist or similar (optional)

build.gradle:

apply plugin: 'groovy'

dependencies {	
	compile 'com.googlecode.json-simple:json-simple:1.1.1'

	testCompile 'org.slf4j:slf4j-api:1.7.25'
	testCompile 'org.codehaus.groovy:groovy-all:2.4.11'
	testCompile 'org.spockframework:spock-core:1.1-groovy-2.4'
	testRuntime 'com.athaydes:spock-reports:1.2.7'
}

RequestProcessor.java

package com.amimas.requests

import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;

public class RequestProcessor {
	private String user;
	private String location;
	private String request;

	public void setRequest(String request) {
		this.request = request;
	}
	public String getRequest() {
		return request;
	}

	public void validateRequest() throws NullPointerException {
		JSONParser parser = new JSONParser();
		try {
			JSONObject json = (JSONObject) parser.parse(this.getRequest());
			if(json.isEmpty() || json.size() != 2) {
				throw new Exception("invalid request");
			}
			aUser 	= (String) json.get("user");
			alocation = (String) json.get("location");
			
			if(aUser.trim().isEmpty() || alocation.trim().isEmpty()) {
					throw new Exception("invalid request format");
			}
		} catch (ParseException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
        }
}

RequestProcessorSpec.groovy

	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

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

testCompile 'org.spockframework:spock-core:1.1-groovy-2.4'
@sdoeringNew
Copy link

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.

@amimas
Copy link
Author

amimas commented Jun 4, 2018

You're right. Thank you for the explanation.

@amimas amimas closed this as completed Jun 4, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants