Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Register Tags Active Topics RSS Search Mark Forums Read

Sponsored Links



Junit - mock static method

Software Development


Reply
 
Thread Tools Search this Thread
  #1  
Old 29-07-2010
Member
 
Join Date: Jan 2010
Posts: 70
Junit - mock static method
  

Hello,
Recently I was using the easy mock to write all my junit test for the functions and classes. But the problem is that the library has very few interfaces and interfaces can not be mocked easily, then require much effort. Now can you please let me know that how can I mock up a static method, I am very new to this. Also, any more discussion on this is interest me. Thank you.

Reply With Quote
  #2  
Old 29-07-2010
Member
 
Join Date: Dec 2009
Posts: 263
Re: Junit - mock static method

Consider the class Foo:

Code:
<?php
cl Foo
{
    public static function dsmthg()
    {
        return static::hpr();
    }
 
    public static function hpr()
    {
        return 'foo';
    }
}
?>
When testing Foo::dsmthg() we want to decouple it from its dependency Foo::hpr(). With PHPUnit 3.5 and PHP 5.3 as well as consistent use of late static binding (using static:: instead of self: the following is possible:

Code:
<?php
cl FooTest extends PHPUnit_Framework_TestCase
{
    public function tdsmthg()
    {
        $cl = $this->getMockClass(
          'Foo',          /* name of cl to mock     */
          array('hlpr') /* list of methods to mock   */
        );
 
        $cl::staticExpects($this->any())
              ->method('hlpr')
              ->will($this->returnValue('bar'));
 
        $this->assertEquals(
          'bar',
          $cl::doSomething()
        );
    }
}
?>
The new staticExpects() method works similar to the non-static expects() variant.

This approach only works for the stubbing and mocking of static method calls where caller and called are in the same cl. This is because static methods are death to testability.

Note : Unit-Testing needs seams, seams is where we prevent the execution of normal code path and is how we achieve isolation of the class under test. Seams work through polymorphism, we override/implement class/interface and then wire the class under test differently in order to take control of the execution flow. With static methods there is nothing to override. Yes, static methods are easy to call, but if the static method calls another static method there is no way to override the called method dependency.
Reply With Quote
  #3  
Old 29-07-2010
Member
 
Join Date: Apr 2008
Posts: 242
Re: Junit - mock static method

When you create unit tests are not always possible to test a component completely isolated. Components have relationships to other components and must communicate with them to solve a specific task. A method that performs a search requires a database. Without these can not perform the operation. But what if there are any database? A mock object simulates a predefined behavior. The object to test draws on the mock object instead of the real object to. Probably the most common in the Framework. NET world is Rhino Mocks.
Reply With Quote
  #4  
Old 29-07-2010
Member
 
Join Date: Nov 2009
Posts: 330
Re: Junit - mock static method

I found was PowerMock. PowerMock is actually an extension for the popular mocking frameworks EasyMock and Mockito. PowerMock provides additional functionality especially for testing “legacy code”, that was not designed for testing (e.g. mocking of static members, private members, partial mocking, and more). And PowerMock also supports mocking of classes in signed packages out of the box. To use PowerMock, you have to use the PowerMockRunner to execute the tests and tell PowerMock which classes need special treatment. Also most calls to the static methods of EasyMock have to be replaced with calls of the equivalent methods.
Reply With Quote
  #5  
Old 10-08-2010
Member
 
Join Date: Sep 2009
Posts: 143
Re: Junit - mock static method

The principle described here is the name of State Verification. The Mock Framework is used to create placeholder objects and simulate its behavior towards other components. The review of the test method by comparing its return value with an expected result. This is usually done on Assert methods.
Reply With Quote
  #6  
Old 11-08-2010
Member
 
Join Date: Sep 2009
Posts: 143
Re: Junit - mock static method

This contrasts with the so-called interaction or Behavior Verification Tests. Through such a test can be checked whether a particular object behaves as desired. For example, you can verify that a particular method is called only once, and a certain parameter is used. Such in-depth analysis was not with the traditional unit testing approach possible. It takes no more assert methods. The review is performed by the Mock framework.
Reply With Quote
Reply

  TechArena Community > Software > Software Development
Tags: , , , , , ,



Thread Tools Search this Thread
Search this Thread:

Advanced Search


Similar Threads for: "Junit - mock static method"
Thread Thread Starter Forum Replies Last Post
What is static method in java? Vaibhav S Software Development 5 25-09-2011 09:40 AM
Static method in Abstract class Anthony12 Software Development 6 12-08-2010 10:22 AM
Why main method is static in java? MKAIF Software Development 5 06-03-2010 03:20 PM
Inheritance and polymorphism in static method Vodka Software Development 5 04-03-2010 10:46 AM
When to Declare a static method in JAVA Aamin Software Development 2 04-02-2009 06:25 PM


All times are GMT +5.5. The time now is 03:42 PM.