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