Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error with import of org.junit.Assert

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

    }
like image 666
Leo Avatar asked Apr 09 '17 10:04

Leo


People also ask

What is org JUnit assert fail?

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.

What is org JUnit assert?

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.

How do I import assert into Intellij?

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.


2 Answers

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.*;
like image 144
cassiomolin Avatar answered Sep 24 '22 12:09

cassiomolin


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());
like image 38
7H3_H4CK3R Avatar answered Sep 21 '22 12:09

7H3_H4CK3R