Results 1 to 6 of 6

Thread: Junit - mock static method

  1. #1
    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.

  2. #2
    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.

  3. #3
    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.

  4. #4
    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.

  5. #5
    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.

  6. #6
    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.

Similar Threads

  1. What is static method in java?
    By Vaibhav S in forum Software Development
    Replies: 5
    Last Post: 25-09-2011, 09:40 AM
  2. Static method in Abstract class
    By Anthony12 in forum Software Development
    Replies: 6
    Last Post: 12-08-2010, 10:22 AM
  3. Why main method is static in java?
    By MKAIF in forum Software Development
    Replies: 5
    Last Post: 06-03-2010, 03:20 PM
  4. Inheritance and polymorphism in static method
    By Vodka in forum Software Development
    Replies: 5
    Last Post: 04-03-2010, 10:46 AM
  5. When to Declare a static method in JAVA
    By Aamin in forum Software Development
    Replies: 2
    Last Post: 04-02-2009, 06:25 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •