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());
}
}
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.*;
You should add the keyword static
to import it. An example:
import static org.junit.Assert.assertFalse;
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());
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.