Skip to content

Commit

Permalink
feat: unit tests, update junit, add mockito & add test job (#1291)
Browse files Browse the repository at this point in the history
  • Loading branch information
pajlada authored Oct 1, 2023
1 parent f5ff616 commit 939860d
Show file tree
Hide file tree
Showing 3 changed files with 116 additions and 1 deletion.
14 changes: 13 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -31,4 +39,8 @@ sourceCompatibility = "11"
tasks.withType(JavaCompile) {
options.encoding = 'UTF-8'
}

tasks.test {
useJUnitPlatform()
}
targetCompatibility = JavaVersion.VERSION_11
49 changes: 49 additions & 0 deletions src/test/java/com/questhelper/MockedTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* Copyright (c) 2023, pajlada <https://github.com/pajlada>
* Copyright (c) 2023, pajlads <https://github.com/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 <a href="https://github.com/pajlads/DinkPlugin/blob/d7c0d4d3f044c25bcff256efc5217955ec1c1494/src/test/java/dinkplugin/notifiers/MockedNotifierTest.java">Dink's MockedNotifierTest</a>
*/
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));
}

}
54 changes: 54 additions & 0 deletions src/test/java/com/questhelper/MockedTestBase.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
* Copyright (c) 2023, pajlada <https://github.com/pajlada>
* Copyright (c) 2023, pajlads <https://github.com/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 <a href="https://github.com/pajlads/DinkPlugin/blob/master/src/test/java/dinkplugin/MockedTestBase.java">Dink's MockedTestBase</a>
*/
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();
}

}

0 comments on commit 939860d

Please sign in to comment.