Showing posts with label constructor. Show all posts
Showing posts with label constructor. Show all posts

Wednesday, June 16, 2010

How to test methods of a class whos constructor is evil, using PowerMock

As promised in my previous post, today I will show, the second way to write test cases for methods of a class whose constructor does crazy stuff.
Example of "crazy stuff" would include opening a db connection, load a native library etc. There are many things that would come under the "crazy" umbrella but lets not go there.

Code under test:

You remember our dear friend Mr. LegacyCustomer right? Yes this class has an evil constructor that does some crazy stuff, in our case it throws a RuntimeException. Effectively, this means we can never create an instance of this class, as there is not constructor that we could invoke.

One of the way to test the method getCreditLimit would be to suppress the constructor of LegacyCustomer using the suppress method which we have already seen in the previous post. So what is the other method to test it?

(On the side note, most Mocking framework don't even have one way to write test for the above code but PowerMock's has two ways in which we could test the above code! How awesome!)

How would we test it - How do they do it!

Turns out, there is a class called Whitebox in PowerMocks using which we could instantiate a class without invoking its constructor. Whitebox class has a lot of features to bypass encapsulation of a class but we will discuss those features later. Today lets focus only on instantiating the class and testing the getCreditLimit method.

To instantiate the class using Whitebox class we use the following syntax

The newInstance method on the Whitebox class takes the name of the class that has to be instantiated without calling the constructor.

Now writing the complete test case would be tad simple. Lets look at the complete test code

Note that we don't even need to use the @RunWith and @PrepareForTest annotations. Whitebox class can instantiate LegacyCustomer without even invoking the constructor.

Please note: If we had to write the test case using the suppress method we would have to use the @RunWith and @PrepareForTest annotations.

Life looks a lot better when we have powerful tools like PowerMock (with Mockito api) at hand!

Monday, June 7, 2010

How to supress methods and constructors using PowerMock

We have come a long way in our how-to series, demonstrating the power of PowerMocks (with the Mockito api).  I hope you are liking it as much as I like writing about it!

In the earlier post we saw how we could partly mock classes and its private methods using PowerMocks.  Today I will demonstrate how we could suppress a constructor and a method.

Now you must be thinking why the hell would I every want to do that!  Well believe me there are cases where you would want to do that :)  And we are about to have a look at one

Code under test:

Now you understand why we might need to suppress a constructor?  Well as we can see in the above code class LegacyCustomer's constructor simply throws RuntimeException (this is just a place holder.  In real world it might do some complicated operation may be loads a native library, establishes a connection to the db god knows what all it can do!).  Customer class extends LegacyCustomer class to add some more behavior to it (I am so making it up!).  In real life, I would never write such code.  Believe me!

But lets assume for now that we want to write a test case to verify that, when the Customer object is created it sets the email field to the value passed as argument.

So how do we test this?

How will we test it - How do they do it!:

We can easily suppress the constructor of LegacyCustomer class.
"Suppress a constructor" means that the code in the constructor will not be called and thats exactly what we want.  The syntax to do that is

Lets read the above line in English.
Suppress the constructor of LegacyCustomer class which takes one argument of String type.  Nice job!
This is step 1 of the process.

Now, if you think for a moment, base class constructor will be invoked automatically when derived class instance is created right!  Hence, in this case LegacyCustomer class's constructor will be invoked as soon as we create an instance of Customer class.

We dont want that to happen!
We want to suppress the call to the constructor of LegacyCustomer class.  So we will have to change the byte code of Customer class and tell it not to invoke the Constructor of LegacyCustomer class.

How do we do that?  Well, you know the answer to that using the following line of code

PowerMocks is happy to do the all byte code manipulation for us, we just need to tell it to do so.  This is done via our very own @PrepareForTest annotation.

So lets look at the entire test case

Simple?

Want more?

What if, I wanted to test a method in Customer class that actually invokes another method that does some complicated stuff?

Lets look at the code to test:

Well its basically the same code as above we have just added the getNetAssetValue method to the Customer class and we want to test this method.

How do we test it - How do they do it!

One way to do that is of-course by using spy and then stubbing the private method.

The other way is to suppress the call to doSomeComplexCalculation method.  What is the syntax for doing that?

Again, lets try to read the above syntax in English language.
Suppress the method of Customer class whose name is doSomeComplexCalculation
 Nice!

Lets look at the complete test case


That's all folks!

Looks like now we know how to suppress any constructor or a method!  Well, what if we wanted to test some method in LegacyCustomer class?  Remember the LegacyCustomer class constructor throws a RuntimeException.

Hummmm, that's interesting how can we suppress the constructor of class under test.  Turns out there are two ways of doing it.  One of the way is what we have just seen, using the suppress method.  Will show the other way in the next post.

Till then, Have Fun with PowerMocks!

Tuesday, May 25, 2010

How to mock constructors using PowerMock

In the how-to series today I will show how-to mock constructors using PowerMocks - Mocks on Steroids!.  In my previous post we saw how can we write unit-tests for mocking final methods and classes.

Requirement:

Again, taking a fictitious example, lets say that ServiceA creates an instance of ServiceB and invokes a businessOperation on ServiceB's instance.  ServiceB's constructor is evil and it throws a Exception.  We want to test this code.  Lets look at the code under test

As we can see, ServiceA's doSomething method create an instance of ServiceB class (by invoking a constructor with no arguments) and then invokes doBusinessOperation() method.  Looks like a simple class, but wait, have a look at ServiceB's constructor, its evil, it throws an exception!  Now how do we mock the constructor so that we can write a good tests case for ServiceA's doSomething method?  Simple, Using PowerMocks (Mockito api) :)

How will we test it - How do they do it!:

We have to use @PrepareForTest annotation with ServiceA class as, it instanciates ServiceB and we actually want to inject a stub on ServiceB so that we can verify that doBusinessOperation method was actually invoked.

Then, we use the whenNew method of PowerMockito to actually return a stub of ServiceB when a new instance of ServiceB is created using a no argument constructor.

Just read the above line in and then compare the syntax below

We can actually read the code in English and it does make a lot of sense!

Lets look at the test code now:

Piece of cake isn't!
Have some Fun!