Reputation: 101
I'm honing/getting my feet back under me doing some simple code challenges in java and making sure I can assert both when the code works and when it throws an exception.
When I test with something that should pass, the code passes correctly. When I have it throw an exception, it throws the exception. But when I use assertThrows on the exception, the test fails with this error:
java.lang.NoSuchMethodError: 'java.lang.Throwable org.junit.Assert.assertThrows(java.lang.Class, org.junit.function.ThrowingRunnable)'
The test code looks like:
package codeChallenges;
import org.junit.jupiter.api.Test;
import java.util.NoSuchElementException;
import static codeChallenges.TwoSum.twoSum;
import static org.junit.Assert.*;
public class TwoSumTest {
@Test
public void failingTwoSumTest() throws NoSuchElementException {
Integer[] testArray = new Integer[2];
Integer firstInt = 3;
Integer secondInt = 7;
testArray[0] = firstInt;
testArray[1] = secondInt;
assertThrows(NoSuchElementException.class, ()-> twoSum(testArray, 20));
}
the actual code:
package codeChallenges;
import java.util.*;
public class TwoSum {
public static int[] twoSum(Integer[] nums, int target) throws NoSuchElementException {
int[] solution = new int[]{-1, -1};
HashMap<Integer, Integer> numMap= new HashMap<>();
for (int i = 0; i < nums.length; i++) {
int complement = target - nums[i];
if (numMap.containsKey(complement)) {
solution[0] = i;
solution[1] = numMap.get(complement);
return solution;
}
numMap.put(nums[i], i);
}
throw new NoSuchElementException("a solution is not present");
}
}
And here are/is the dependencies/build.gradle file:
/*
* This file was generated by the Gradle 'init' task.
*
* This generated file contains a sample Java Library project to get you started.
* For more details take a look at the Java Libraries chapter in the Gradle
* User Manual available at https://docs.gradle.org/6.6.1/userguide/java_library_plugin.html
*/
plugins {
// Apply the java-library plugin to add support for Java Library
id 'java-library'
}
repositories {
// Use jcenter for resolving dependencies.
// You can declare any Maven/Ivy/file repository here.
jcenter()
}
dependencies {
// This dependency is exported to consumers, that is to say found on their compile classpath.
api 'org.apache.commons:commons-math3:3.6.1'
// This dependency is used internally, and not exposed to consumers on their own compile classpath.
implementation 'com.google.guava:guava:29.0-jre'
implementation 'org.junit.jupiter:junit-jupiter:5.4.2'
implementation 'org.junit.jupiter:junit-jupiter:5.4.2'
// Use JUnit test framework
testImplementation 'junit:junit:4.13'
}
I've invalidated cache and restarted, I moved the code out of this specific project into its own... does the method being static
impact if it is a runnable or not? I'm at a loss here. Thank you for the help in advance.
Upvotes: 0
Views: 2406
Reputation: 19979
assertThrows(NoSuchElementException.class, ()-> TwoSum.twoSum(testArray, 20));
You are not calling the method from class but was trying to call directly , use TwoSum.twoSum instead
Upvotes: 2