You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@camel.apache.org by Иванов Григорий - <Iv...@esphere.ru> on 2020/10/22 09:54:11 UTC
Mocking a class in a script
Hi
I have a production route with a groovy script I should test, is there a way to mock URL class or do something else to not really make a request to production API? The problem is that besides the request part it have some other stuff I would like to be tested.
I agree, this route is not as testable as it could be, but for now it comes "as is". Could you suggest something?
<route id="route.with.http.in.sctipt">
<from uri="direct:in"/>
<script>
<groovy>
... some stuff I should test ...
def r = new URL("http://someproductoinservice.com").openConnection();
r.with {
setRequestMethod("POST")
setConnectTimeout(60000)
setReadTimeout(300000)
setDoOutput(true)
setRequestProperty("Content-Type", "application/json")
getOutputStream().write(request.getHeader('inMsg').getBytes("UTF-8"));
}
r.getResponseCode();
... more stuff ...
</groovy>
</script>
...
</route>
Re: Mocking a class in a script
Posted by Mantas Gridinas <mg...@gmail.com>.
Id suggest providing URL as an argument, but this looks too complex to be
shoved in a groovy script.
On Thu, Oct 22, 2020, 12:54 Иванов Григорий - <Iv...@esphere.ru> wrote:
>
> Hi
>
> I have a production route with a groovy script I should test, is there a
> way to mock URL class or do something else to not really make a request to
> production API? The problem is that besides the request part it have some
> other stuff I would like to be tested.
>
> I agree, this route is not as testable as it could be, but for now it
> comes "as is". Could you suggest something?
>
>
> <route id="route.with.http.in.sctipt">
>
> <from uri="direct:in"/>
> <script>
> <groovy>
> ... some stuff I should test ...
> def r = new URL("http://someproductoinservice.com
> ").openConnection();
> r.with {
> setRequestMethod("POST")
> setConnectTimeout(60000)
> setReadTimeout(300000)
> setDoOutput(true)
> setRequestProperty("Content-Type", "application/json")
>
> getOutputStream().write(request.getHeader('inMsg').getBytes("UTF-8"));
> }
> r.getResponseCode();
> ... more stuff ...
> </groovy>
> </script>
> ...
> </route>
>
>