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










share|improve this question


















  • 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

















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










share|improve this question


















  • 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















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










share|improve this question













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






share|improve this question













share|improve this question











share|improve this question




share|improve this question










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 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
















  • 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










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














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
}
}





share|improve this answer





















  • 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










  • 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











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














 

draft saved


draft discarded


















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

























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
}
}





share|improve this answer





















  • 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










  • 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















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
}
}





share|improve this answer





















  • 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










  • 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













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
}
}





share|improve this answer












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
}
}






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 7 at 9:11









Szymon Stepniak

15.7k83061




15.7k83061












  • 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










  • 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


















  • 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










  • 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
















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


















 

draft saved


draft discarded



















































 


draft saved


draft discarded














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





















































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







這個網誌中的熱門文章

Xamarin.form Move up view when keyboard appear

Post-Redirect-Get with Spring WebFlux and Thymeleaf

Anylogic : not able to use stopDelay()