From 0ecf6d8d138306f272aa26c4a5e468f796c3c2fa Mon Sep 17 00:00:00 2001 From: Dean Brettle Date: Fri, 19 Apr 2024 11:54:26 -0700 Subject: [PATCH] Update to Mockito 5. Also allow mocking of final methods and update associated test. Switching to Mockito 5 should reduce the likelihood that we run into issues related to using earlier versions of Mockito with the increasingly stringent security restrictions of new JDK versions. --- build.gradle | 226 +++++++++--------- .../org/carlmontrobotics/lib199/Mocks.java | 2 +- .../carlmontrobotics/lib199/MocksTest.java | 6 +- 3 files changed, 117 insertions(+), 117 deletions(-) diff --git a/build.gradle b/build.gradle index 0e2a9c1a..ed5e83cb 100644 --- a/build.gradle +++ b/build.gradle @@ -1,113 +1,113 @@ -plugins { - id "java" - id "edu.wpi.first.GradleRIO" version "2024.3.1" - id "maven-publish" -} - -java { - sourceCompatibility = JavaVersion.VERSION_17 - targetCompatibility = JavaVersion.VERSION_17 -} - -group 'org.carlmontrobotics' - -def ROBOT_MAIN_CLASS = "" - -// Define my targets (RoboRIO) and artifacts (deployable files) -// This is added by GradleRIO's backing project DeployUtils. -deploy { - targets { - roborio(getTargetTypeClass('RoboRIO')) { - // Team number is loaded either from the .wpilib/wpilib_preferences.json - // or from command line. If not found an exception will be thrown. - // You can use getTeamOrDefault(team) instead of getTeamNumber if you - // want to store a team number in this file. - team = project.frc.getTeamNumber() - debug = project.frc.getDebugOrDefault(false) - - artifacts { - // First part is artifact name, 2nd is artifact type - // getTargetTypeClass is a shortcut to get the class type using a string - - frcJava(getArtifactTypeClass('FRCJavaArtifact')) { - } - - // Static files artifact - frcStaticFileDeploy(getArtifactTypeClass('FileTreeArtifact')) { - files = project.fileTree('src/main/deploy') - directory = '/home/lvuser/deploy' - } - } - } - } -} - -def deployArtifact = deploy.targets.roborio.artifacts.frcJava - -// Set to true to use debug for JNI. -wpi.java.debugJni = false - -// Set this to true to enable desktop support. -def includeDesktopSupport = true - -// Defining my dependencies. In this case, WPILib (+ friends), and vendor libraries. -// Also defines JUnit 5. -dependencies { - implementation wpi.java.deps.wpilib() - implementation wpi.java.vendor.java() - - roborioDebug wpi.java.deps.wpilibJniDebug(wpi.platforms.roborio) - roborioDebug wpi.java.vendor.jniDebug(wpi.platforms.roborio) - - roborioRelease wpi.java.deps.wpilibJniRelease(wpi.platforms.roborio) - roborioRelease wpi.java.vendor.jniRelease(wpi.platforms.roborio) - - nativeDebug wpi.java.deps.wpilibJniDebug(wpi.platforms.desktop) - nativeDebug wpi.java.vendor.jniDebug(wpi.platforms.desktop) - simulationDebug wpi.sim.enableDebug() - - nativeRelease wpi.java.deps.wpilibJniRelease(wpi.platforms.desktop) - nativeRelease wpi.java.vendor.jniRelease(wpi.platforms.desktop) - simulationRelease wpi.sim.enableRelease() - - testImplementation 'junit:junit:4.13.2' - - implementation 'org.mockito:mockito-core:3.6.0' - implementation group: 'org.apache.commons', name: 'commons-csv', version: '1.6' -} - -// Simulation configuration (e.g. environment variables). -wpi.sim.addGui().defaultEnabled = true -wpi.sim.addDriverstation() - -// Setting up my Jar File. In this case, adding all libraries into the main jar ('fat jar') -// in order to make them all available at runtime. Also adding the manifest so WPILib -// knows where to look for our Robot Class. -jar { - from { configurations.runtimeClasspath.collect { it.isDirectory() ? it : zipTree(it) } } - from sourceSets.main.allSource - manifest edu.wpi.first.gradlerio.GradleRIOPlugin.javaManifest(ROBOT_MAIN_CLASS) - duplicatesStrategy = DuplicatesStrategy.INCLUDE -} - -publishing { - publications { - gpr(MavenPublication) { - groupId = 'org.carlmontrobotics' - artifactId = 'lib199' - version = '4.0.2' - - from components.java - } - } -} - -// Configure jar and deploy tasks -deployArtifact.jarTask = jar -wpi.java.configureExecutableTasks(jar) -wpi.java.configureTestTasks(test) - -// Configure string concat to always inline compile -tasks.withType(JavaCompile) { - options.compilerArgs.add '-XDstringConcat=inline' -} +plugins { + id "java" + id "edu.wpi.first.GradleRIO" version "2024.3.1" + id "maven-publish" + } + +java { + sourceCompatibility = JavaVersion.VERSION_17 + targetCompatibility = JavaVersion.VERSION_17 +} + +group 'org.carlmontrobotics' + +def ROBOT_MAIN_CLASS = "" + +// Define my targets (RoboRIO) and artifacts (deployable files) +// This is added by GradleRIO's backing project DeployUtils. +deploy { + targets { + roborio(getTargetTypeClass('RoboRIO')) { + // Team number is loaded either from the .wpilib/wpilib_preferences.json + // or from command line. If not found an exception will be thrown. + // You can use getTeamOrDefault(team) instead of getTeamNumber if you + // want to store a team number in this file. + team = project.frc.getTeamNumber() + debug = project.frc.getDebugOrDefault(false) + + artifacts { + // First part is artifact name, 2nd is artifact type + // getTargetTypeClass is a shortcut to get the class type using a string + + frcJava(getArtifactTypeClass('FRCJavaArtifact')) { + } + + // Static files artifact + frcStaticFileDeploy(getArtifactTypeClass('FileTreeArtifact')) { + files = project.fileTree('src/main/deploy') + directory = '/home/lvuser/deploy' + } + } + } + } +} + +def deployArtifact = deploy.targets.roborio.artifacts.frcJava + +// Set to true to use debug for JNI. +wpi.java.debugJni = false + +// Set this to true to enable desktop support. +def includeDesktopSupport = true + +// Defining my dependencies. In this case, WPILib (+ friends), and vendor libraries. +// Also defines JUnit 5. +dependencies { + implementation wpi.java.deps.wpilib() + implementation wpi.java.vendor.java() + + roborioDebug wpi.java.deps.wpilibJniDebug(wpi.platforms.roborio) + roborioDebug wpi.java.vendor.jniDebug(wpi.platforms.roborio) + + roborioRelease wpi.java.deps.wpilibJniRelease(wpi.platforms.roborio) + roborioRelease wpi.java.vendor.jniRelease(wpi.platforms.roborio) + + nativeDebug wpi.java.deps.wpilibJniDebug(wpi.platforms.desktop) + nativeDebug wpi.java.vendor.jniDebug(wpi.platforms.desktop) + simulationDebug wpi.sim.enableDebug() + + nativeRelease wpi.java.deps.wpilibJniRelease(wpi.platforms.desktop) + nativeRelease wpi.java.vendor.jniRelease(wpi.platforms.desktop) + simulationRelease wpi.sim.enableRelease() + + testImplementation 'junit:junit:4.13.2' + + implementation 'org.mockito:mockito-core:5.11.0' + implementation group: 'org.apache.commons', name: 'commons-csv', version: '1.6' +} + +// Simulation configuration (e.g. environment variables). +wpi.sim.addGui().defaultEnabled = true +wpi.sim.addDriverstation() + +// Setting up my Jar File. In this case, adding all libraries into the main jar ('fat jar') +// in order to make them all available at runtime. Also adding the manifest so WPILib +// knows where to look for our Robot Class. +jar { + from { configurations.runtimeClasspath.collect { it.isDirectory() ? it : zipTree(it) } } + from sourceSets.main.allSource + manifest edu.wpi.first.gradlerio.GradleRIOPlugin.javaManifest(ROBOT_MAIN_CLASS) + duplicatesStrategy = DuplicatesStrategy.INCLUDE +} + +publishing { + publications { + gpr(MavenPublication) { + groupId = 'org.carlmontrobotics' + artifactId = 'lib199' + version = '4.0.2' + + from components.java + } + } +} + +// Configure jar and deploy tasks +deployArtifact.jarTask = jar +wpi.java.configureExecutableTasks(jar) +wpi.java.configureTestTasks(test) + +// Configure string concat to always inline compile +tasks.withType(JavaCompile) { + options.compilerArgs.add '-XDstringConcat=inline' +} diff --git a/src/main/java/org/carlmontrobotics/lib199/Mocks.java b/src/main/java/org/carlmontrobotics/lib199/Mocks.java index f20db5d8..18cc06de 100644 --- a/src/main/java/org/carlmontrobotics/lib199/Mocks.java +++ b/src/main/java/org/carlmontrobotics/lib199/Mocks.java @@ -85,7 +85,7 @@ public static T createMock(Class classToMock, U implClass, boolean for public static T createMock(Class classToMock, U implClass, Answer defaultAnswer, Class... interfaces) { HashMap methods = new HashMap<>(); for(Method m: listMethods(classToMock, interfaces)) { - if(Modifier.isStatic(m.getModifiers()) || Modifier.isFinal(m.getModifiers())) { + if(Modifier.isStatic(m.getModifiers())) { continue; } try { diff --git a/src/test/java/org/carlmontrobotics/lib199/MocksTest.java b/src/test/java/org/carlmontrobotics/lib199/MocksTest.java index b202e7d5..d6703c11 100644 --- a/src/test/java/org/carlmontrobotics/lib199/MocksTest.java +++ b/src/test/java/org/carlmontrobotics/lib199/MocksTest.java @@ -22,7 +22,7 @@ public void testSimpleMock() { assertFalse(mock instanceof TestInterface); assertEquals(1, mock.test1()); assertEquals(3, mock.test2()); - assertEquals(3, mock.test3()); + assertEquals(4, mock.test3()); } @Test @@ -37,7 +37,7 @@ public void testNullDefaultAnswer() { TestBase mock = Mocks.createMock(TestBase.class, new TestImpl(), false); assertEquals(0, mock.test1()); assertEquals(3, mock.test2()); - assertEquals(3, mock.test3()); + assertEquals(4, mock.test3()); } @Test @@ -45,7 +45,7 @@ public void testCustomDefaultAnswer() { TestBase mock = Mocks.createMock(TestBase.class, new TestImpl(), invocation -> 5); assertEquals(5, mock.test1()); assertEquals(3, mock.test2()); - assertEquals(3, mock.test3()); + assertEquals(4, mock.test3()); } public class TestBase {