9951 explained code solutions for 126 technologies


phpunitHow to use PHPUnit assert to check void?


PHPUnit provides an assertNull() method to check if a value is null. This method can be used to check if a void method returns null.

<?php

class MyClass
{
    public function voidMethod()
    {
        // do something
    }
}

$myClass = new MyClass();

$this->assertNull($myClass->voidMethod());

The code above will check if the voidMethod() returns null.

  1. $myClass = new MyClass(); - creates an instance of the MyClass class.
  2. $this->assertNull($myClass->voidMethod()); - calls the assertNull() method to check if the voidMethod() returns null.

Helpful links

Edit this code on GitHub