Gradle DSL method not found: 'testImplementation()'

Hi. I am trying to add unit tests to my project and have, as described here

https://developer.android.com/training/testing/unit-testing/local-unit-tests

added the line
testImplementation ‘junit:junit:4.12’ to the dependencies section of the build.gradle file. However I get the following error:

Gradle DSL method not found: ‘testImplementation()’
Possible causes:
The project ‘Patterns’ may be using a version of the Android Gradle plug-in that does not contain the method (e.g. ‘testCompile’ was added in 1.1.0).
Upgrade plugin to version 4.0.0 and sync project

The project ‘Patterns’ may be using a version of Gradle that does not contain the method.
Open Gradle wrapper file

The build file may be missing a Gradle plugin.
Apply Gradle plugin

I can’t find any resolution for this on Google, although there are dozens of similar questions. Thanks.