PHP Unit Test Mocking a member variable dependency - unit-testing

Hello lets say I want to test the function run from Class A and I'm using Mockery to mock external dependencies:
class A {
protected myB;
public function __construct(B $param) {
$this->myB = $param;
}
protected function doStuff() {
return "done";
}
public function run() {
$this->doStuff();
$this->myB->doOtherStuff();
return "finished";
}
}
class B {
public function doOtherStuff() {
return "done";
}
}
So I wrote the test like this:
public function testRun() {
$mockB = Mockery::mock('overload:B');
$mockB->shouldReceive('doOtherStuff')
->andReturn("not done");
$mockA = Mockery::mock(A::class)->makePartial()->shouldAllowMockingProtectedMethods();
$mockA->shouldReceive('doStuff')->andReturns("done");
$mockA->run();
}
This throws me an exception like this:
Error: Call to a member function doStuff() on null
I tried diffrent variations of mocking the internal dependency B which is getting called in the run function but I always endend up in an exception.
What am I doing wrong here?

Don't mock the thing you are testing. Inject the mocked B into A.
public function testRun()
{
// Arrange
$mockB = Mockery::mock(B::class);
$a = new A($mockB);
// Assert
$mockB->shouldReceive('doOtherStuff')
->once()
->andReturn("not done");
// Act
$a->run();
}
If you want to monkey patch A, you can still pass the mocked B in (more details: constructor arguments in mockery):
public function testRun()
{
// Arrange
$mockB = Mockery::mock(B::class);
$a = Mockery::mock(A::class, [$mockB])
->makePartial()
->shouldAllowMockingProtectedMethods();
$a->shouldReceive('doStuff')
->andReturns('mocked done');
// Assert
$mockB->shouldReceive('doOtherStuff')
->once()
->andReturn('not done');
// Act
$a->run();
}

Related

How to unit test a method which has CompletedFeature<UserDefinedObject>.supplyAsync(()-> someMethodWithReturn())) using Mockito?

I have a controller class with a method something() which makes calls to two different methods of the same class and merge the result of two calls.
class Controller{
...
public UDC doSomething(){
CompletableFuture<UDC> feature1 = CompletableFuture.supplyAsync(()-> {this.doOther()}).exceptionally(ex -> {return new SomeException();});
CompletableFuture<UDC> feature2 = CompletableFuture.supplyAsync(()-> {this.doSomeOther()}).exceptionally(ex -> {return new SomeException();});
...
return feature1.combine(feature2).get();
}
...
}
I don't think you should use Mockito to mock CompletableFuture here, any of them...
In the test, treat the Controller's doSomething functionality as a black box that given some input returns UDC.
Now, it's possible that doOther and/or doSomeOther call some external code that should be mocked. In this case the Controller looks probably something like this:
class Controller {
private final SomeExternalDependency dependency1;
public Controller(SomeExternalDependency dependency1) {
this.dependency1 = dependency1;
}
private UDC doOther() {
...
dependency1.foo();
...
}
private UDC toSomeOther() {
...
dependency1.bar();
...
}
}
In this case in the test you can mock out the dependency1 with mockito as usual:
class MyTest {
#Test
public void doSomething() {
SomeExternalDependency dep = Mockito.mock(SomeExternalDependency.class);
// specify the expectations
Controller controller = new Controller(dep);
controller.doSomething();
}
}

Why doesn't dynamic partial mocking work for JMockit's #Injectable?

In the following test case where no Expectations have been recorded, I would expect that the dynamic partial mocking feature will be used for the fields A and B which are initialized in UnitToTest using #Injectable. But instead always the method calls are mocked. Only using an invalid filter value for static partial mocking, it is possible to call the real methods:
#Service
class A {
public String doSomething() { return "doSomething"; }
public String doSomethingElse() { return "doSomethingElse"; }
}
#Service
class B {
public String doSomething() { return "doSomething"; }
public String doSomethingElse() { return "doSomethingElse"; }
}
#Service
class UnitToTest {
#Autowired B b;
#Autowired A a;
public B getB() { return b; }
public A getA() { return a; }
}
public class TestClass {
#Tested UnitToTest unit;
// #Mocked({ "someInvalidFilter()" })
#Injectable A a;
// #Mocked({ "someInvalidFilter()" })
#Injectable B b;
#Test
public void test() {
// actual return value is always null if no invalid static partial
// mocking filters are specified above
assertEquals("doSomething", unit.getA().doSomething());
assertEquals("doSomethingElse", unit.getA().doSomethingElse());
assertEquals("doSomething", unit.getB().doSomething());
assertEquals("doSomethingElse", unit.getB().doSomethingElse());
}
}
For me it looks like dynamic partial mocking with JMockit doesn't work for #Injectables. Is that a known restriction?
#Injectables always get injected into #Tested objects, assuming a matching field or constructor parameter can be found; the injection process even takes into consideration DI annotations such as #Inject and #Autowired.
However, an #Injectable instance is always created as an uninitialized (ie, with no state) and fully mocked instance. Partial mocking, on the other hand, is meant for real instances that you instantiate (and initialize) yourself in the test.
So, what you seem to be asking for is that said real instances (partially mocked or not) could be injected into #Tested objects. Indeed, this is not supported (except by calling Deencapsulation.setField), since a motivating use case was never presented by users.
That said, the example test will pass if it is changed to the following:
public class TestClass {
#Tested(fullyInitialized = true) UnitToTest unit;
#Test
public void test() {
assertEquals("doSomething", unit.getA().doSomething());
assertEquals("doSomethingElse", unit.getA().doSomethingElse());
assertEquals("doSomething", unit.getB().doSomething());
assertEquals("doSomethingElse", unit.getB().doSomethingElse());
}
}
The above is an integration test, though, not a unit test.

ZF2 & Doctrine2: mock EntityRepository::findOneByField

Is it possible to mock the
EntityRepository::findOneBy{$field}($value)
function?
Using:
->getMock('EntityRepository')
->expects($this->any())
->method('findOneByField')
resolves always in null, as findOneByField isn't a real function but gets mapped by __call(). At least I think that that is the problem..
You can mock the __call method. Will give a simple example:
Class:
class A {
public function __call($fname, $args) {
if($fname === 'test') {
return 'test';
}
}
}
Test:
class ATest extends PHPUnit_Framework_TestCase
{
public function testA() {:
$mock = $this->getMock('A');
$mock->expects($this->any())
->method('__call')
->with('test')
->will($this->returnValue('test'));
var_dump($mock->test());
}
}
You can do the same with the EntityRepository. I just hadn't one by the hand for testing.

how to create a mock in a model test case

Maybe I am doing this wrong.
I'd like to test the beforeSave method of a model (Antibody). A part of this method calls a method on an associated model (Species). I'd like to mock the Species model but don't find how.
Is it possible or am I doing something that goes against the MVC pattern and thus trying to do something that I shouldn't?
class Antibody extends AppModel {
public function beforeSave() {
// some processing ...
// retreive species_id based on the input
$this->data['Antibody']['species_id']
= isset($this->data['Species']['name'])
? $this->Species->getIdByName($this->data['Species']['name'])
: null;
return true;
}
}
Assuming your Species model in created by cake due to relations, you can simply do something like this:
public function setUp()
{
parent::setUp();
$this->Antibody = ClassRegistry::init('Antibody');
$this->Antibody->Species = $this->getMock('Species');
// now you can set your expectations here
$this->Antibody->Species->expects($this->any())
->method('getIdByName')
->will($this->returnValue(/*your value here*/));
}
public function testBeforeFilter()
{
// or here
$this->Antibody->Species->expects($this->once())
->method('getIdByName')
->will($this->returnValue(/*your value here*/));
}
Well, it depends on the way your 'Species' object is injected.
Is it injected via the constructor ? Via a setter ? Is it inherited ?
Here is an example with a constructor injected object :
class Foo
{
/** #var Bar */
protected $bar;
public function __construct($bar)
{
$this->bar = $bar;
}
public function foo() {
if ($this->bar->isOk()) {
return true;
} else {
return false;
}
}
}
Then your test would be something like this:
public function test_foo()
{
$barStub = $this->getMock('Overblog\CommonBundle\TestUtils\Bar');
$barStub->expects($this->once())
->method('isOk')
->will($this->returnValue(false));
$foo = new Foo($barStub);
$this->assertFalse($foo->foo());
}
The process is quite the same with setter injected objects :
public function test_foo()
{
$barStub = $this->getMock('Overblog\CommonBundle\TestUtils\Bar');
$barStub->expects($this->once())
->method('isOk')
->will($this->returnValue(false));
$foo = new Foo();
$foo->setBar($barStub);
$this->assertFalse($foo->foo());
}

NUnit Conditional Teardown?

Is there a way to do a conditional TearDown in NUnit?
I have a TestFixture which has a need to run cleanup code for just a few tests, and I don't really want to:
Run the TearDown method on every test
Create a private helper method and call it from the tests requiring cleanup if I can avoid it
There isn't unfortunately.
Can you not do the cleanup in the [TestFixtureTearDown] instead, so once all the tests have finished? I guess that depends on whether the cleanup has to be done before the next test runs.
Alternatively, put those tests that require a cleanup in another class/TextFixture together, away from the other tests. Then you can use a TearDown in there which doesn't need to be conditional.
Edit:
One thing I've just thought of, which could be done to achieve the aim though probably isn't actually worth it for this particular need, is that you can extend NUnit - create your own custom attributes which you could handle however you wanted. This is mentioned here. Like I say, I don't think really you should go down that route for this, but is useful to know none-the-less
You can have the main TearDown in a base class:
[TearDown]
public virtual void TearDown()
{
// Tear down things here
}
and then override it in the class where you have the tests that should not run the tear down code:
[TearDown]
public override void TearDown()
{
// By not calling base.TearDown() here you avoid tearing down
}
Extend all you classes with test from BaseTest
public class BaseTest
{
[SetUp]
public void BeforeTest()
{
GetService<NUnitHooksController>().ExecuteBeforeTestHooks(this);
}
[TearDown]
public void AfterTest()
{
GetService<NUnitHooksController>().ExecuteAfterTestHooks(this);
}
}
Use AfterTest and BeforeTest hooks. Works both with and without category.
public class ExampleTest : BaseTest
{
[Test, Category("asdasd")]
public void Test01()
{
...
}
[AfterTest("asdasd")]
public void ExampleHook()
{
...
}
}
public class NUnitHooksController
{
private readonly ILogger _log;
public NUnitHooksController(ILogger log)
{
_log = log;
}
public void ExecuteBeforeTestHooks(object testClass)
{
ExecuteHooks(testClass, typeof(BeforeTestAttribute));
}
public void ExecuteAfterTestHooks(object testClass)
{
ExecuteHooks(testClass, typeof(AfterTestAttribute));
}
private MethodInfo[] GetHookMethods(object currentTestClass, Type attributeType)
{
return currentTestClass
.GetType()
.GetMethods()
.Where(m => m.GetCustomAttributes(attributeType, false).Length > 0)
.ToArray();
}
private void ExecuteHooks(object testClass, Type requiredAttributeType)
{
var hooks = GetHookMethods(testClass, requiredAttributeType);
var testCategories = GetTestCategories();
foreach (var hook in hooks)
{
var allAttributes = hook.GetCustomAttributes(requiredAttributeType, true);
foreach (var attribute in allAttributes)
{
if (!attribute.GetType().IsEquivalentTo(requiredAttributeType))
{
continue;
}
var hookCategories = GetCategoriesFromAttribute(attribute);
// if we do not have specific category on hook
// or we have at least one same category on hook and test
if (!hookCategories.Any() || hookCategories.Intersect(testCategories).Any())
{
ExecuteHookMethod(testClass, hook);
}
}
}
}
private object[] GetTestCategories()
{
return TestContext.CurrentContext.Test.Properties["Category"].ToArray();
}
private void ExecuteHookMethod(object testClass, MethodInfo method)
{
var hookName = method.Name;
_log.Information($"Executing - '{hookName}' hook");
try
{
method.Invoke(testClass, Array.Empty<object>());
}
catch (Exception e)
{
_log.Error($"Executing of - '{hookName}' hook failed - {e}");
}
}
private string[] GetCategoriesFromAttribute(object attribute)
{
if (attribute is BeforeTestAttribute beforeTestAttribute)
{
return beforeTestAttribute.Categories;
}
if (attribute is AfterTestAttribute afterTestAttribute)
{
return afterTestAttribute.Categories;
}
throw new ArgumentException($"{attribute.GetType().FullName} - does not have categories");
}
}
I have solved this using the name of the test:
namespace TestProject
{
public class TestClass
{
// Test without TearDown
[Test]
public void Test1()
{
Assert.Pass("Test1 passed");
}
// Test with TearDown
[Test]
public void Test2()
{
Assert.Pass("Test2 passed");
}
[TearDown]
public void TearDown()
{
// Execute only after Test2
if (TestContext.CurrentContext.Test.Name.Equals(nameof(this.Test2)))
{
// Execute Test2 TearDown...
}
}
}
}
Or if you want to use the full name of Test2 (TestProject.TestClass.Test2) you can replace the line
if (TestContext.CurrentContext.Test.Name.Equals(nameof(this.Test2)))
by
if (TestContext.CurrentContext.Test.FullName.Equals(typeof(TestClass).FullName + "." nameof(this.Test2)))