Ability of Spock to intercept creation of object inside methdods

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Ability of Spock to intercept creation of object inside methdods

Andrii Pischanski
Hi, everyone,

I am new in Spock as well as in Groovy...

Do someone know whether Spock able to intercept and replace creation of object inside the private method. (please do not suggest to refactor. I know that this code smells not good but I am in jail of EJB 2.1 and not able to refactor)

So I have the code like the following (I am using Java if any):

class SomeComplexClass {
   
SomeComplexClass() {}

   
public void testedMethod() {
       
// some logic here
        anotherMethod
();
       
// again some logic
   
}

   
private anotherMethod() {
       
// some logic
       
Properties props = new Properties();
       
// adding values to props

       
Context ctx = new InitialContext(props);
       
SomeBean bean = (SomeBean)ctx.lookup("some.bean.Instance");
       
// again some logic
   
}
}

As you see I nedd to create InitialContext (Weblogic initial context if any) but this invocation fails because this context is not available in current space. It throws something like IndexArrrayOutOfBounds - and this is normally due to internal logic of InitialContext creation.

My goal is to mock invocation of:
SomeBean bean = (SomeBean)ctx.lookup("some.bean.Instance");



So I decided to mock creation of InitialContext (as well as Properties). I know that this easily doable using PowerMock for instance.

PowerMock.expectNer(InitialContext.class, props)

But how I may deal with Spock? Whether this is available to do like this? I have tried to use Spy(..) but not sure that Spy(..) able to do this.

Any ideas?

--
You received this message because you are subscribed to the Google Groups "Spock Framework - User" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
Visit this group at http://groups.google.com/group/spockframework.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Ability of Spock to intercept creation of object inside methdods

Przemysław Wojnowski
Hi.

You can mix Spock with Powermock. See samples in my GH repo:
https://github.com/pwojnowski/poligon-spockowy

When you have Spock with PowerMock then you use PM as in any other case.

Sorry for late reply. :-|

Cheers,
Przemysław

2015-06-25 18:31 GMT+02:00 Andrii Pischanski <[hidden email]>:
Hi, everyone,

I am new in Spock as well as in Groovy...

Do someone know whether Spock able to intercept and replace creation of object inside the private method. (please do not suggest to refactor. I know that this code smells not good but I am in jail of EJB 2.1 and not able to refactor)

So I have the code like the following (I am using Java if any):

class SomeComplexClass {
   
SomeComplexClass() {}

   
public void testedMethod() {
       
// some logic here
        anotherMethod
();
       
// again some logic
   
}

   
private anotherMethod() {
       
// some logic
       
Properties props = new Properties();
       
// adding values to props

       
Context ctx = new InitialContext(props);
       
SomeBean bean = (SomeBean)ctx.lookup("some.bean.Instance");
       
// again some logic
   
}
}

As you see I nedd to create InitialContext (Weblogic initial context if any) but this invocation fails because this context is not available in current space. It throws something like IndexArrrayOutOfBounds - and this is normally due to internal logic of InitialContext creation.

My goal is to mock invocation of:
SomeBean bean = (SomeBean)ctx.lookup("some.bean.Instance");



So I decided to mock creation of InitialContext (as well as Properties). I know that this easily doable using PowerMock for instance.

PowerMock.expectNer(InitialContext.class, props)

But how I may deal with Spock? Whether this is available to do like this? I have tried to use Spy(..) but not sure that Spy(..) able to do this.

Any ideas?

--
You received this message because you are subscribed to the Google Groups "Spock Framework - User" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
Visit this group at http://groups.google.com/group/spockframework.
For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "Spock Framework - User" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
Visit this group at http://groups.google.com/group/spockframework.
For more options, visit https://groups.google.com/d/optout.
Loading...