An Error: Call to a member function make() on null in Laravel 8.5 PHPUnit test

When you override the setUP() method on your own, you need to call the parent::setUp() method

In my case, we implemented our own setUp() method, but did not call parent::setUp().

The documentation says the following.

If you define your own setUp / tearDown methods within a test class, be sure to call the respective parent::setUp() / parent::tearDown() methods on the parent class.

https://laravel.com/docs/8.x/testing#creating-and-running-tests

I solved this problem by calling parent::setUp() inside a setUp() method I wrote.

<?php

namespace Tests\Feature;

use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Foundation\Testing\WithFaker;
use Tests\TestCase;

class UserTest extends TestCase
{
    protected function setUp():void {
        parent::setUp(); //I had to add this line.
        //Custom setups are performed after the setUp() process of the parent class
    }
    protected function tearDown():void {
        //Custom cleanup is performed before the parent class tearDown() process
        parent::tearDown();
    }

    /**
     * A basic feature test example.
     *
     * @return void
     */
    public function testExample()
    {
        $response = $this->get('/');

        $response->assertStatus(200);
    }
}

If you want to provide a tearDown method, you need to use parent::tearDown() as well.

More details

In my case, I had our own setUp() method, but I did not call parent::setUp() as shown below.

<?php

namespace Tests\Feature;

use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Foundation\Testing\WithFaker;
use Tests\TestCase;

class UserTest extends TestCase
{
    protected function setUp():void {
        //I forgot to write parent::setUp();.
        //Custom setups are performed after the setUp() process of the parent class
    }
    /**
     * A basic feature test example.
     *
     * @return void
     */
    public function testExample()
    {
        $response = $this->get('/');

        $response->assertStatus(200);
    }
}

In this state, an error occurred when calling $this->get(‘/’).

1) Tests\Feature\UserTest::testExample
Error: Call to a member function make() on null

/var/www/vendor/laravel/framework/src/Illuminate/Foundation/Testing/Concerns/MakesHttpRequests.php:498
/var/www/vendor/laravel/framework/src/Illuminate/Foundation/Testing/Concerns/MakesHttpRequests.php:292
/var/www/tests/Feature/UserTest.php:21

Laravel provides its own useful features for automated testing.

In my case , $this->get(‘/’), is one such example.

A test class created by artisan make:test command is prepared to use such a useful feature.

php artisan make:test UserTest

This test class does not have a setUp() method by default.

In this state, PHPUnit will execute the setUP() method of the parent class. This sets up some useful features.

However, if you implement the setUp() method on your own, it will not do what it is supposed to do in the parent class.

To fix this, we need to write a manual call to the setUp() method of the parent class.