Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
how to mock the new object creation in the method
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  11 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
ML  
View profile  
 More options Oct 10 2011, 3:59 pm
From: ML <mal...@gmail.com>
Date: Mon, 10 Oct 2011 12:59:31 -0700 (PDT)
Local: Mon, Oct 10 2011 3:59 pm
Subject: how to mock the new object creation in the method
I want to test the method using the mockito (not using annotations);

my method looks like this:

public void methodName(){
//do some logic

Object object = new Object(otherObject);
//do some logic

}

I want to test this function but I want to ignore -mock new object
creation somehow.

and the second question: can I mock static methods.
I have somewhere in the code Class.staticMethod that I would like to
mock.
Is that possible? I've read somewhere that's not possible with
mockito. is that true?
Thanks a lot.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Greg Silin  
View profile  
 More options Oct 10 2011, 4:10 pm
From: Greg Silin <gregsi...@gmail.com>
Date: Mon, 10 Oct 2011 13:10:17 -0700
Local: Mon, Oct 10 2011 4:10 pm
Subject: Re: [mockito] how to mock the new object creation in the method

If you absolutely need to mock a constructor invocation, AFAIK the only way
to do it is with Powermock framework's whenNew.  See more here:
http://www.gitshah.com/2010/05/how-to-mock-constructors-using.html


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Brice Dutheil  
View profile  
 More options Oct 10 2011, 4:19 pm
From: Brice Dutheil <brice.duth...@gmail.com>
Date: Mon, 10 Oct 2011 22:19:50 +0200
Local: Mon, Oct 10 2011 4:19 pm
Subject: Re: [mockito] how to mock the new object creation in the method

Yes it is not.

Mockito empowers lowly coupled object design, if you mock static methods or
constructors you are creating coupled code. COupled design is known to be
not good for evolutivity and maintainability. You really should avoid such
design choices, and choose other alternatives.

For example if you want to avoid this object creation, why not instead
inject a Factory or a Proxy (as in the GoF), or even Providers (such as in
Guice).

Anyway in certain circumstances you might be forced to do that, and then
Powermock will help.

--
Brice


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
David Wallace  
View profile  
 More options Oct 11 2011, 2:16 am
From: David Wallace <dmwallace...@gmail.com>
Date: Mon, 10 Oct 2011 23:16:33 -0700 (PDT)
Local: Tues, Oct 11 2011 2:16 am
Subject: Re: how to mock the new object creation in the method
I know of two really good ways of doing this with Mockito.  Both
require rearranging your code a little, to make it more testable.
Writing code that's easily testable is a good thing to be doing,
regardless of which mocking framework you end up using.

Pattern 1 involves factoring uses of "new" into one-line methods, then
using a Mockito spy.  This is the simpler of the two patterns.
Pattern 2 involves factoring uses of "new" into a separate class and
injecting it.  It's a little more work, but it can be more powerful.
The "new" calls that you want to factor out (using either pattern) are
those where an object is created, that you are likely to want to
mock.  I recommend using one or other of these patterns, whenever you
find yourself writing "new", on a class that's in your code base (as
opposed to a JDK class or a library class).

To use pattern 1 (testing a class called MyClass), you would replace a
call like
Foo foo = new Foo( a, b, c );
with
Foo foo = makeFoo( a, b, c );
and write a one-line method
protected Foo makeFoo( A a, B b, C c ){
    return new Foo( a, b, c );

}

It's important that you don't include any logic in the method; just
the one line that creates the object.  Then, when you come to test the
class, the object that you test will actually be a Mockito spy, with
this method overridden, to return a mock.  So your test class might
contain members like
@Mock private Foo mockFoo;
private MyClass toTest = spy( new MyClass());

Lastly, inside your test method you mock out the call to makeFoo with
a line like
when( toTest.makeFoo( any( A.class ), any( B.class ),
any( C.class ))).thenReturn( mockFoo );
You can use matchers that are more specific than any() if you want to
check the arguments that are passed to the constructor.

Note that I haven't tested the code above, so it may contain typos,
but the basic pattern works.

One case where this pattern won't work is if MyClass is final.  Most
of the Mockito framework doesn't play particularly well with final
classes; and this includes the use of spy().  Another case is where
MyClass uses getClass() somewhere, and requires the value of it to be
MyClass.  This won't work, because the class of a spy is actually a
Mockito-generated subclass of the original class.  In either of these
cases, you'll need the slightly more robust FactoryHelper pattern, as
follows (again, this may contain typos).

public class MyClass{
    protected static class FactoryHelper{
        Foo makeFoo( A a, B b, C c ){
            return new Foo( a, b, c );
        }
    }

    ...

    private FactoryHelper helper;
    public MyClass( X x, Y y ){
        this( x, y, new FactoryHelper());
    }

    protected MyClass( X x, Y, y, FactoryHelper helper ){

        ...

        this.helper = helper;
    }

    ...

    Foo foo = helper.makeFoo( a, b, c );

}

So, you have a special constructor, just for testing, that has an
additional argument.  This is used from your test class, when creating
the object that you're going to test.  In your test class, you mock
the FactoryHelper class, as well as the object that you want to
create.

@Mock private MyClass.FactoryHelper mockFactoryHelper;
@Mock private Foo mockFoo;
private MyClass toTest;

and you can use it like this

toTest = new MyClass( x, y, mockFactoryHelper );
when( mockFactoryHelper.makeFoo( any( A.class ), any( B.class ),
any( C.class ))).thenReturn( mockFoo );

As before, you can use matchers that are more specific than any() if
you need to.

I realise I've skipped over some things, in an effort not to make this
post too long.  Don't hesitate to ask if something is unclear.  But
don't abandon Mockito because it lacks PowerMock's whenNew() - you
don't need it.

Hope this helps.
David.

On Oct 11, 8:59 am, ML <mal...@gmail.com> wrote:


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
ML  
View profile   Translate to Translated (View Original)
 More options Oct 11 2011, 3:35 pm
From: ML <mal...@gmail.com>
Date: Tue, 11 Oct 2011 12:35:43 -0700 (PDT)
Local: Tues, Oct 11 2011 3:35 pm
Subject: Re: how to mock the new object creation in the method
thank you, guys for your great comments and explanations. I've dot it
finally. =)
I've managed to run it using Mockito only and a little re-design of
code.
Your comments helped me a lot in that.
They've made me think a lot about the design of code I'm writing.
Definitely have to extend my knowledge in TDD.
Thanks you.

 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Bhushan Madan  
View profile  
 More options Dec 20 2012, 12:15 pm
From: Bhushan Madan <bhushan.ma...@gmail.com>
Date: Thu, 20 Dec 2012 09:15:36 -0800 (PST)
Local: Thurs, Dec 20 2012 12:15 pm
Subject: Re: how to mock the new object creation in the method

Thanks David. I have been hunting for solution to this problem and you just
made my day great!!


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Jeff Vincent  
View profile  
 More options Dec 20 2012, 4:31 pm
From: Jeff Vincent <predato...@gmail.com>
Date: Thu, 20 Dec 2012 13:31:30 -0800 (PST)
Local: Thurs, Dec 20 2012 4:31 pm
Subject: Re: how to mock the new object creation in the method

I really like this list of things that help make code more testable, but it isn't always obvious how to implement these in all cases.  Regardless there are some good concepts to think about...

http://googletesting.blogspot.com/2008/08/by-miko-hevery-so-you-decid...


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Brice Dutheil  
View profile  
 More options Dec 21 2012, 6:24 am
From: Brice Dutheil <brice.duth...@gmail.com>
Date: Fri, 21 Dec 2012 12:24:34 +0100
Local: Fri, Dec 21 2012 6:24 am
Subject: Re: [mockito] Re: how to mock the new object creation in the method

Great pointer, Misko Hevery's blog is full of useful stuff, it brings back
memories of this blog entry.

-- Brice


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
José Matias  
View profile  
 More options Jan 31, 4:13 am
From: José Matias <jose.p.mat...@gmail.com>
Date: Thu, 31 Jan 2013 01:13:25 -0800 (PST)
Local: Thurs, Jan 31 2013 4:13 am
Subject: Re: how to mock the new object creation in the method

Hello great tips on refactoring, alltough i came to the following situation
where my newly created
object can throw an exception, such as FileNotFoundException

so my testing code

EmailAlertsSender spyEmailSender = spy(new EmailAlertsSender());
 FileOutputStream output = mock(FileOutputStream.class);

-->
when(spyEmailSender.makeFileOutputStream(anyString())).thenReturn(output);

when invoking the 'when' throws a FileNotFoundException

@Override
public FileOutputStream makeFileOutputStream(String fileLocation) throws
FileNotFoundException{
return new FileOutputStream(fileLocation);

}

allthough it works if i dont throw the exception

@Override
public FileOutputStream makeFileOutputStream(String fileLocation){
try {
return new FileOutputStream(fileLocation);

}

catch (FileNotFoundException e) {
return null;

}
}

i would like to be able to throw the exception, what can i do?
Cheers

 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Eric Lefevre-Ardant  
View profile  
 More options Jan 31, 5:19 am
From: Eric Lefevre-Ardant <e...@ericlefevre.net>
Date: Thu, 31 Jan 2013 11:19:21 +0100
Local: Thurs, Jan 31 2013 5:19 am
Subject: Re: [mockito] Re: how to mock the new object creation in the method

Yes, then when(mock.method()) pattern does not work well in those
situations.

Try doReturn(output).when(spyEmailSender).makeFileOutputStream(anyString())
instead.

Eric

Le jeudi 31 janvier 2013, José Matias a écrit :


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Brice Dutheil  
View profile  
 More options Jan 31, 8:25 am
From: Brice Dutheil <brice.duth...@gmail.com>
Date: Thu, 31 Jan 2013 14:25:45 +0100
Local: Thurs, Jan 31 2013 8:25 am
Subject: Re: [mockito] Re: how to mock the new object creation in the method

Yes that's how it should be done.

Here's why, for spies, the real code is executed by default, so when you
write this line

when(spyEmailSender.makeFileOutputStream(anyString())).thenReturn(output);

Here's how it is decomposed :

String arg1 = anyString();
FileOutputStream fos = spyEmailSender.makeFileOutputStream(arg1);
when(fos).thenReturn(output);

So as this is a spy the real code of your method gets executed, and receive
null from the anyString() matcher.

A contrario, if you use the other syntax that Eric suggested, the real code
won't be executed while stubbing your interactions, because the mock is
place in a specific mode before calling the stubbed method.

Hope that helps.

Always thank you Eric for monitoring the chan and helping people :)

-- Brice

On Thu, Jan 31, 2013 at 11:19 AM, Eric Lefevre-Ardant
<e...@ericlefevre.net>wrote:


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »