groovy spock prints stacktrace though exception is catched
up vote
1
down vote
favorite
I have a spock test, which calls some service:
when:
def x = service.call()
then:
!x
Inside service I have:
def call() {
try {
doCall() // method, which throws an exception
} catch (e) {
false
}
}
So I expect method to return false
and test to pass. However, test invocation from IDE prints stacktrace of the exception, though it is catched.
Purpose of the test is not to know whether exception was thrown, but just assert returned result is false, so I don't want to use Specification.thrown
groovy junit spock
add a comment |
up vote
1
down vote
favorite
I have a spock test, which calls some service:
when:
def x = service.call()
then:
!x
Inside service I have:
def call() {
try {
doCall() // method, which throws an exception
} catch (e) {
false
}
}
So I expect method to return false
and test to pass. However, test invocation from IDE prints stacktrace of the exception, though it is catched.
Purpose of the test is not to know whether exception was thrown, but just assert returned result is false, so I don't want to use Specification.thrown
groovy junit spock
2
what exception do you have?catch(e)
catches only Exception and descendants.
– daggett
Nov 7 at 8:04
My exception isGroovyRuntimeException
– Rahul Katrakhtalapali
Nov 7 at 10:27
Btw, post a full stack trace please. Otherwise it is impossible to guess what might be the real cause of the issue.
– Szymon Stepniak
Nov 7 at 11:05
What Spock version, Groovy version an Java version are you using? In Spock 1.1 and 1.2 under Java 8 the behavior is correct. And as @SzymonStepniak said - post also the full stack trace.
– cgrim
Nov 7 at 11:06
The problem is ingroovyx.net.http.HTTPBuilder:494
(0.7.1 version). It prints warn message and passes caught exception there, which prints stacktrace and then rethrows it
– Rahul Katrakhtalapali
Nov 7 at 12:07
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
I have a spock test, which calls some service:
when:
def x = service.call()
then:
!x
Inside service I have:
def call() {
try {
doCall() // method, which throws an exception
} catch (e) {
false
}
}
So I expect method to return false
and test to pass. However, test invocation from IDE prints stacktrace of the exception, though it is catched.
Purpose of the test is not to know whether exception was thrown, but just assert returned result is false, so I don't want to use Specification.thrown
groovy junit spock
I have a spock test, which calls some service:
when:
def x = service.call()
then:
!x
Inside service I have:
def call() {
try {
doCall() // method, which throws an exception
} catch (e) {
false
}
}
So I expect method to return false
and test to pass. However, test invocation from IDE prints stacktrace of the exception, though it is catched.
Purpose of the test is not to know whether exception was thrown, but just assert returned result is false, so I don't want to use Specification.thrown
groovy junit spock
groovy junit spock
asked Nov 7 at 7:21
Rahul Katrakhtalapali
4211615
4211615
2
what exception do you have?catch(e)
catches only Exception and descendants.
– daggett
Nov 7 at 8:04
My exception isGroovyRuntimeException
– Rahul Katrakhtalapali
Nov 7 at 10:27
Btw, post a full stack trace please. Otherwise it is impossible to guess what might be the real cause of the issue.
– Szymon Stepniak
Nov 7 at 11:05
What Spock version, Groovy version an Java version are you using? In Spock 1.1 and 1.2 under Java 8 the behavior is correct. And as @SzymonStepniak said - post also the full stack trace.
– cgrim
Nov 7 at 11:06
The problem is ingroovyx.net.http.HTTPBuilder:494
(0.7.1 version). It prints warn message and passes caught exception there, which prints stacktrace and then rethrows it
– Rahul Katrakhtalapali
Nov 7 at 12:07
add a comment |
2
what exception do you have?catch(e)
catches only Exception and descendants.
– daggett
Nov 7 at 8:04
My exception isGroovyRuntimeException
– Rahul Katrakhtalapali
Nov 7 at 10:27
Btw, post a full stack trace please. Otherwise it is impossible to guess what might be the real cause of the issue.
– Szymon Stepniak
Nov 7 at 11:05
What Spock version, Groovy version an Java version are you using? In Spock 1.1 and 1.2 under Java 8 the behavior is correct. And as @SzymonStepniak said - post also the full stack trace.
– cgrim
Nov 7 at 11:06
The problem is ingroovyx.net.http.HTTPBuilder:494
(0.7.1 version). It prints warn message and passes caught exception there, which prints stacktrace and then rethrows it
– Rahul Katrakhtalapali
Nov 7 at 12:07
2
2
what exception do you have?
catch(e)
catches only Exception and descendants.– daggett
Nov 7 at 8:04
what exception do you have?
catch(e)
catches only Exception and descendants.– daggett
Nov 7 at 8:04
My exception is
GroovyRuntimeException
– Rahul Katrakhtalapali
Nov 7 at 10:27
My exception is
GroovyRuntimeException
– Rahul Katrakhtalapali
Nov 7 at 10:27
Btw, post a full stack trace please. Otherwise it is impossible to guess what might be the real cause of the issue.
– Szymon Stepniak
Nov 7 at 11:05
Btw, post a full stack trace please. Otherwise it is impossible to guess what might be the real cause of the issue.
– Szymon Stepniak
Nov 7 at 11:05
What Spock version, Groovy version an Java version are you using? In Spock 1.1 and 1.2 under Java 8 the behavior is correct. And as @SzymonStepniak said - post also the full stack trace.
– cgrim
Nov 7 at 11:06
What Spock version, Groovy version an Java version are you using? In Spock 1.1 and 1.2 under Java 8 the behavior is correct. And as @SzymonStepniak said - post also the full stack trace.
– cgrim
Nov 7 at 11:06
The problem is in
groovyx.net.http.HTTPBuilder:494
(0.7.1 version). It prints warn message and passes caught exception there, which prints stacktrace and then rethrows it– Rahul Katrakhtalapali
Nov 7 at 12:07
The problem is in
groovyx.net.http.HTTPBuilder:494
(0.7.1 version). It prints warn message and passes caught exception there, which prints stacktrace and then rethrows it– Rahul Katrakhtalapali
Nov 7 at 12:07
add a comment |
1 Answer
1
active
oldest
votes
up vote
0
down vote
First of all - check what specific exception gets thrown and gets outside the try-catch
block. You have to be aware that following code in Groovy:
try {
// do something
} catch (e) {
// do something with exception
}
is an equivalent of:
try {
// do something
} catch (Exception e) {
// do something with exception
}
It means that java.lang.Throwable
and all its children classes (except java.lang.Exception
) are not caught inside your try catch. For instance:
def call() {
try {
throw new Error('Something wrong happened')
} catch (e) {
false
}
}
This exception won't get caught by try-catch
and you will see something like this in the console log:
java.lang.Error: Something wrong happened
at com.github.wololock.micronaut.TestSpec$Service.call(TestSpec.groovy:22)
at com.github.wololock.micronaut.TestSpec.test(TestSpec.groovy:13)
It happens, because java.lang.Error
extends java.lang.Throwable
and it is not a child class of java.lang.Exception
.
If you want to catch all possible exceptions that may happened to your code you would have to use Throwable
inside the catch block, something like this:
def call() {
try {
throw new Error('Something wrong happened')
} catch (Throwable e) {
false
}
}
My exception is of typeGroovyRuntimeException
, which is a child ofException
– Rahul Katrakhtalapali
Nov 7 at 10:28
I think it is spock-related, as when I just run app, not tests, exception is caught and there is no stacktrace
– Rahul Katrakhtalapali
Nov 7 at 10:29
It doesn't make any sense. ThrowingGroovyRuntimeException
from the same place asdoCall()
method is being called gets caught bytry-catch
correctly. Check the implementation ofdoCall
method. Maybe you have some kind oftry-catch
applied there so it catches the exception and prints stacktrace inside the method. Alternatively, double check if the exception you see is nothing else thanGroovyRuntimeException
. It may happen thatGroovyRuntimeException
is nested inside otherThrowable
and then it is not caught by yourtry-catch
.
– Szymon Stepniak
Nov 7 at 11:05
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
First of all - check what specific exception gets thrown and gets outside the try-catch
block. You have to be aware that following code in Groovy:
try {
// do something
} catch (e) {
// do something with exception
}
is an equivalent of:
try {
// do something
} catch (Exception e) {
// do something with exception
}
It means that java.lang.Throwable
and all its children classes (except java.lang.Exception
) are not caught inside your try catch. For instance:
def call() {
try {
throw new Error('Something wrong happened')
} catch (e) {
false
}
}
This exception won't get caught by try-catch
and you will see something like this in the console log:
java.lang.Error: Something wrong happened
at com.github.wololock.micronaut.TestSpec$Service.call(TestSpec.groovy:22)
at com.github.wololock.micronaut.TestSpec.test(TestSpec.groovy:13)
It happens, because java.lang.Error
extends java.lang.Throwable
and it is not a child class of java.lang.Exception
.
If you want to catch all possible exceptions that may happened to your code you would have to use Throwable
inside the catch block, something like this:
def call() {
try {
throw new Error('Something wrong happened')
} catch (Throwable e) {
false
}
}
My exception is of typeGroovyRuntimeException
, which is a child ofException
– Rahul Katrakhtalapali
Nov 7 at 10:28
I think it is spock-related, as when I just run app, not tests, exception is caught and there is no stacktrace
– Rahul Katrakhtalapali
Nov 7 at 10:29
It doesn't make any sense. ThrowingGroovyRuntimeException
from the same place asdoCall()
method is being called gets caught bytry-catch
correctly. Check the implementation ofdoCall
method. Maybe you have some kind oftry-catch
applied there so it catches the exception and prints stacktrace inside the method. Alternatively, double check if the exception you see is nothing else thanGroovyRuntimeException
. It may happen thatGroovyRuntimeException
is nested inside otherThrowable
and then it is not caught by yourtry-catch
.
– Szymon Stepniak
Nov 7 at 11:05
add a comment |
up vote
0
down vote
First of all - check what specific exception gets thrown and gets outside the try-catch
block. You have to be aware that following code in Groovy:
try {
// do something
} catch (e) {
// do something with exception
}
is an equivalent of:
try {
// do something
} catch (Exception e) {
// do something with exception
}
It means that java.lang.Throwable
and all its children classes (except java.lang.Exception
) are not caught inside your try catch. For instance:
def call() {
try {
throw new Error('Something wrong happened')
} catch (e) {
false
}
}
This exception won't get caught by try-catch
and you will see something like this in the console log:
java.lang.Error: Something wrong happened
at com.github.wololock.micronaut.TestSpec$Service.call(TestSpec.groovy:22)
at com.github.wololock.micronaut.TestSpec.test(TestSpec.groovy:13)
It happens, because java.lang.Error
extends java.lang.Throwable
and it is not a child class of java.lang.Exception
.
If you want to catch all possible exceptions that may happened to your code you would have to use Throwable
inside the catch block, something like this:
def call() {
try {
throw new Error('Something wrong happened')
} catch (Throwable e) {
false
}
}
My exception is of typeGroovyRuntimeException
, which is a child ofException
– Rahul Katrakhtalapali
Nov 7 at 10:28
I think it is spock-related, as when I just run app, not tests, exception is caught and there is no stacktrace
– Rahul Katrakhtalapali
Nov 7 at 10:29
It doesn't make any sense. ThrowingGroovyRuntimeException
from the same place asdoCall()
method is being called gets caught bytry-catch
correctly. Check the implementation ofdoCall
method. Maybe you have some kind oftry-catch
applied there so it catches the exception and prints stacktrace inside the method. Alternatively, double check if the exception you see is nothing else thanGroovyRuntimeException
. It may happen thatGroovyRuntimeException
is nested inside otherThrowable
and then it is not caught by yourtry-catch
.
– Szymon Stepniak
Nov 7 at 11:05
add a comment |
up vote
0
down vote
up vote
0
down vote
First of all - check what specific exception gets thrown and gets outside the try-catch
block. You have to be aware that following code in Groovy:
try {
// do something
} catch (e) {
// do something with exception
}
is an equivalent of:
try {
// do something
} catch (Exception e) {
// do something with exception
}
It means that java.lang.Throwable
and all its children classes (except java.lang.Exception
) are not caught inside your try catch. For instance:
def call() {
try {
throw new Error('Something wrong happened')
} catch (e) {
false
}
}
This exception won't get caught by try-catch
and you will see something like this in the console log:
java.lang.Error: Something wrong happened
at com.github.wololock.micronaut.TestSpec$Service.call(TestSpec.groovy:22)
at com.github.wololock.micronaut.TestSpec.test(TestSpec.groovy:13)
It happens, because java.lang.Error
extends java.lang.Throwable
and it is not a child class of java.lang.Exception
.
If you want to catch all possible exceptions that may happened to your code you would have to use Throwable
inside the catch block, something like this:
def call() {
try {
throw new Error('Something wrong happened')
} catch (Throwable e) {
false
}
}
First of all - check what specific exception gets thrown and gets outside the try-catch
block. You have to be aware that following code in Groovy:
try {
// do something
} catch (e) {
// do something with exception
}
is an equivalent of:
try {
// do something
} catch (Exception e) {
// do something with exception
}
It means that java.lang.Throwable
and all its children classes (except java.lang.Exception
) are not caught inside your try catch. For instance:
def call() {
try {
throw new Error('Something wrong happened')
} catch (e) {
false
}
}
This exception won't get caught by try-catch
and you will see something like this in the console log:
java.lang.Error: Something wrong happened
at com.github.wololock.micronaut.TestSpec$Service.call(TestSpec.groovy:22)
at com.github.wololock.micronaut.TestSpec.test(TestSpec.groovy:13)
It happens, because java.lang.Error
extends java.lang.Throwable
and it is not a child class of java.lang.Exception
.
If you want to catch all possible exceptions that may happened to your code you would have to use Throwable
inside the catch block, something like this:
def call() {
try {
throw new Error('Something wrong happened')
} catch (Throwable e) {
false
}
}
answered Nov 7 at 9:11
Szymon Stepniak
15.7k83061
15.7k83061
My exception is of typeGroovyRuntimeException
, which is a child ofException
– Rahul Katrakhtalapali
Nov 7 at 10:28
I think it is spock-related, as when I just run app, not tests, exception is caught and there is no stacktrace
– Rahul Katrakhtalapali
Nov 7 at 10:29
It doesn't make any sense. ThrowingGroovyRuntimeException
from the same place asdoCall()
method is being called gets caught bytry-catch
correctly. Check the implementation ofdoCall
method. Maybe you have some kind oftry-catch
applied there so it catches the exception and prints stacktrace inside the method. Alternatively, double check if the exception you see is nothing else thanGroovyRuntimeException
. It may happen thatGroovyRuntimeException
is nested inside otherThrowable
and then it is not caught by yourtry-catch
.
– Szymon Stepniak
Nov 7 at 11:05
add a comment |
My exception is of typeGroovyRuntimeException
, which is a child ofException
– Rahul Katrakhtalapali
Nov 7 at 10:28
I think it is spock-related, as when I just run app, not tests, exception is caught and there is no stacktrace
– Rahul Katrakhtalapali
Nov 7 at 10:29
It doesn't make any sense. ThrowingGroovyRuntimeException
from the same place asdoCall()
method is being called gets caught bytry-catch
correctly. Check the implementation ofdoCall
method. Maybe you have some kind oftry-catch
applied there so it catches the exception and prints stacktrace inside the method. Alternatively, double check if the exception you see is nothing else thanGroovyRuntimeException
. It may happen thatGroovyRuntimeException
is nested inside otherThrowable
and then it is not caught by yourtry-catch
.
– Szymon Stepniak
Nov 7 at 11:05
My exception is of type
GroovyRuntimeException
, which is a child of Exception
– Rahul Katrakhtalapali
Nov 7 at 10:28
My exception is of type
GroovyRuntimeException
, which is a child of Exception
– Rahul Katrakhtalapali
Nov 7 at 10:28
I think it is spock-related, as when I just run app, not tests, exception is caught and there is no stacktrace
– Rahul Katrakhtalapali
Nov 7 at 10:29
I think it is spock-related, as when I just run app, not tests, exception is caught and there is no stacktrace
– Rahul Katrakhtalapali
Nov 7 at 10:29
It doesn't make any sense. Throwing
GroovyRuntimeException
from the same place as doCall()
method is being called gets caught by try-catch
correctly. Check the implementation of doCall
method. Maybe you have some kind of try-catch
applied there so it catches the exception and prints stacktrace inside the method. Alternatively, double check if the exception you see is nothing else than GroovyRuntimeException
. It may happen that GroovyRuntimeException
is nested inside other Throwable
and then it is not caught by your try-catch
.– Szymon Stepniak
Nov 7 at 11:05
It doesn't make any sense. Throwing
GroovyRuntimeException
from the same place as doCall()
method is being called gets caught by try-catch
correctly. Check the implementation of doCall
method. Maybe you have some kind of try-catch
applied there so it catches the exception and prints stacktrace inside the method. Alternatively, double check if the exception you see is nothing else than GroovyRuntimeException
. It may happen that GroovyRuntimeException
is nested inside other Throwable
and then it is not caught by your try-catch
.– Szymon Stepniak
Nov 7 at 11:05
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53185036%2fgroovy-spock-prints-stacktrace-though-exception-is-catched%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
2
what exception do you have?
catch(e)
catches only Exception and descendants.– daggett
Nov 7 at 8:04
My exception is
GroovyRuntimeException
– Rahul Katrakhtalapali
Nov 7 at 10:27
Btw, post a full stack trace please. Otherwise it is impossible to guess what might be the real cause of the issue.
– Szymon Stepniak
Nov 7 at 11:05
What Spock version, Groovy version an Java version are you using? In Spock 1.1 and 1.2 under Java 8 the behavior is correct. And as @SzymonStepniak said - post also the full stack trace.
– cgrim
Nov 7 at 11:06
The problem is in
groovyx.net.http.HTTPBuilder:494
(0.7.1 version). It prints warn message and passes caught exception there, which prints stacktrace and then rethrows it– Rahul Katrakhtalapali
Nov 7 at 12:07