I'm having a little problem with a unit-test my professor gave me. Upon compilation, I recieve the following errors:cannot find symbol import org.junit.Assert.assertArrayEquals;
cannot find symbol import org.junit.Assert.assertEquals;
import org.junit.Assert.assertFalse;
import org.junit.Assert.assertTrue;
I have downloaded JUnit and I can compile a similar file, so why am I having problems with this? The code is:
import java.util.Comparator;
import org.junit.Assert.assertArrayEquals;
import org.junit.Assert.assertEquals;
import org.junit.Assert.assertFalse;
import org.junit.Assert.assertTrue;
import org.junit.Before;
import org.junit.Test;
public class SortingTests {
class IntegerComparator implements Comparator<Integer> {
@Override
public int compare(Integer i1, Integer i2) {
return i1.compareTo(i2);
}
}
private Integer i1,i2,i3;
private OrderedArray<Integer> orderedArray;
@Before
public void createOrderedArray(){
i1 = -12;
i2 = 0;
i3 = 4;
orderedArray = new OrderedArray<>(new IntegerComparator());
}
@Test
public void testIsEmpty_zeroEl(){
assertTrue(orderedArray.isEmpty());
}
@Test
public void testIsEmpty_oneEl() throws Exception{
orderedArray.add(i1);
assertFalse(orderedArray.isEmpty());
}
@Test
public void testSize_zeroEl() throws Exception{
assertEquals(0,orderedArray.size());
}
}
Junit fail assertion method fails the test by throwing an assertion error, the fail method is used in junit version 4 and it will use the org. junit. Assert class. The junit fail method is used to verify that the actual exception will throw an error or the test is failing at the time of development.
Definition of JUnit Assert. It is a method that was used for determining the pass and fail status of the test cases. This method was provided by class name as org. JUnit. Assert which was used to extend the java.
Use ⌥⏎ (macOS), or Alt+Enter (Windows/Linux), to import static methods into classes to improve the readability of your code. This can be especially useful when you're migrating from older versions of testing frameworks and need to make multiple replacements in your class.
Assuming that you have the JUnit dependency in the classpath, use import static
for the assert methods:
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
Or simply use:
import static org.junit.Assert.*;
What you are looking for is a Static import
The line import org.junit.Assert.assertArrayEquals;
is referencing the method assertArrayEquals
from the class org.junit.Assert
Importing a static method so that it is callable like assertEquals(0,orderedArray.size());
is done with a static import line. Try out the following:
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
Alternatively you can:
import static org.junit.Assert.*;
, or you could:
import org.junit.Assert;
and reference the methods like
Assert.assertEquals(0,orderedArray.size());
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With