From 939860d579bd60bd0f398711331b63e85fea707e Mon Sep 17 00:00:00 2001 From: pajlada Date: Sun, 1 Oct 2023 11:50:53 +0200 Subject: [PATCH] feat: unit tests, update junit, add mockito & add test job (#1291) --- build.gradle | 14 ++++- src/test/java/com/questhelper/MockedTest.java | 49 +++++++++++++++++ .../java/com/questhelper/MockedTestBase.java | 54 +++++++++++++++++++ 3 files changed, 116 insertions(+), 1 deletion(-) create mode 100644 src/test/java/com/questhelper/MockedTest.java create mode 100644 src/test/java/com/questhelper/MockedTestBase.java diff --git a/build.gradle b/build.gradle index 1eb536a4e3..7629137c8e 100644 --- a/build.gradle +++ b/build.gradle @@ -18,10 +18,18 @@ dependencies { compileOnly 'org.projectlombok:lombok:1.18.4' annotationProcessor 'org.projectlombok:lombok:1.18.4' - testImplementation 'junit:junit:4.12' testImplementation group: 'net.runelite', name:'client', version: runeLiteVersion testImplementation group: 'net.runelite', name:'jshell', version: runeLiteVersion + def junitVersion = "5.5.2" // max version before junit-bom was added to pom files, due to runelite restrictions + testImplementation group: "org.junit.jupiter", name: "junit-jupiter-api", version: junitVersion + testImplementation group: "org.junit.jupiter", name: "junit-jupiter-params", version: junitVersion + testImplementation group: "org.junit.jupiter", name: "junit-jupiter-engine", version: junitVersion + + testImplementation group: 'org.mockito', name:'mockito-core', version: "4.11.0" // runelite uses 3.1.0 + testImplementation(group: 'com.google.inject.extensions', name:'guice-testlib', version: "4.1.0") { + exclude group: 'com.google.inject', module: 'guice' // already provided by runelite + } } group = 'com.questhelper' @@ -31,4 +39,8 @@ sourceCompatibility = "11" tasks.withType(JavaCompile) { options.encoding = 'UTF-8' } + +tasks.test { + useJUnitPlatform() +} targetCompatibility = JavaVersion.VERSION_11 diff --git a/src/test/java/com/questhelper/MockedTest.java b/src/test/java/com/questhelper/MockedTest.java new file mode 100644 index 0000000000..e06a7c1e35 --- /dev/null +++ b/src/test/java/com/questhelper/MockedTest.java @@ -0,0 +1,49 @@ +/* + * Copyright (c) 2023, pajlada + * Copyright (c) 2023, pajlads + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ +package com.questhelper; + +import com.google.inject.testing.fieldbinder.Bind; +import net.runelite.api.Client; +import org.mockito.Mockito; + +/** + * Based on Dink's MockedNotifierTest + */ +public abstract class MockedTest extends MockedTestBase +{ + @Bind + protected Client client = Mockito.mock(Client.class); + + @Override + protected void setUp() + { + super.setUp(); + + // init client mocks + // when(client.getWorldType()).thenReturn(EnumSet.noneOf(WorldType.class)); + } + +} diff --git a/src/test/java/com/questhelper/MockedTestBase.java b/src/test/java/com/questhelper/MockedTestBase.java new file mode 100644 index 0000000000..926992f0cb --- /dev/null +++ b/src/test/java/com/questhelper/MockedTestBase.java @@ -0,0 +1,54 @@ +/* + * Copyright (c) 2023, pajlada + * Copyright (c) 2023, pajlads + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ +package com.questhelper; + +import com.google.inject.Guice; +import com.google.inject.testing.fieldbinder.BoundFieldModule; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.mockito.MockitoAnnotations; + +/** + * Based on Dink's MockedTestBase + */ +public abstract class MockedTestBase +{ + private AutoCloseable mocks; + + @BeforeEach + protected void setUp() + { + this.mocks = MockitoAnnotations.openMocks(this); + Guice.createInjector(BoundFieldModule.of(this)).injectMembers(this); + } + + @AfterEach + protected void cleanUp() throws Exception + { + mocks.close(); + } + +}