6

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());
      }

    }
Leo
  • 241
  • 2
  • 3
  • 12
  • 1
    Probably the jar is not in the classpath. Could you please confirm that? also please tell which jar are you using? – DNAj Apr 09 '17 at 10:43
  • 1
    I'm using JUnit 4.12 and the jar should be in the classpath. I'm able to compile a similar test in the same folder. – Leo Apr 09 '17 at 11:10
  • 1
    Ok, I was making a mistake with the classpath. Thanks for the help. – Leo Apr 09 '17 at 11:39
  • Possible duplicate of [Cannot find symbol assertEquals](http://stackoverflow.com/questions/20631621/cannot-find-symbol-assertequals) – bobbel Apr 11 '17 at 16:17

4 Answers4

3

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.*;
cassiomolin
  • 124,154
  • 35
  • 280
  • 359
3

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());
7H3_H4CK3R
  • 133
  • 8
2

You should add the keyword static to import it. An example:

 import static org.junit.Assert.assertFalse;
Pau
  • 14,917
  • 14
  • 67
  • 94
  • Addind the keyword gives me the following error: package org.junit does not exist – Leo Apr 09 '17 at 11:12
0

If you are using junit version 5 and above then use org.junit.jupiter.api.assertions .

Path has been moved in junit 5

  • 1
    Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Nov 01 '22 at 02:16