2017-04-09 78 views
3

私の教授が私に教えてくれた単体テストに少し問題があります。コンパイル時に、私は次のエラーを受け取る:
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;org.junit.Assertのインポートでエラーが発生しました

私はJUnitのをダウンロードしていると私は似たファイルをコンパイルすることができ、なぜ私はこれで問題が発生していますか? コードは次のとおりです。

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

    } 
+1

のようなメソッドを参照おそらく瓶がありますクラスパスではありません。それを確認していただけますか?どの瓶を使っているのか教えてください。 – DNAj

+0

私はJUnit 4.12を使用していますが、jarはクラスパス内になければなりません。私は同じフォルダで同じようなテストをコンパイルすることができます。 – Leo

+0

私はクラスパスで間違いをしていました。助けてくれてありがとう。 – Leo

答えて

1

あなたはそれをインポートするには、キーワードstaticを追加する必要があります。例:あなたが探しているものを

import static org.junit.Assert.*; 
+0

キーワードを追加すると、次のエラーが表示されます。パッケージorg.junitは存在しません – Leo

3

がアサートメソッドのためimport staticを使用し、あなたがクラスパスにJUnit dependencyを持っていると仮定すると、はです。静的インポート

ラインimport org.junit.Assert.assertArrayEquals;は、assertEquals(0,orderedArray.size());静的インポートラインで行われるような呼び出し可能であるように静的メソッドを読み込むクラスorg.junit.Assert

から方法assertArrayEqualsを参照しています。次のことを試してみてください:

import static org.junit.Assert.assertArrayEquals; 
import static org.junit.Assert.assertEquals; 
import static org.junit.Assert.assertFalse; 
import static org.junit.Assert.assertTrue; 

代わりに次のことができます。

import static org.junit.Assert.*; 

、またはあなたができます

import org.junit.Assert; 

Assert.assertEquals(0,orderedArray.size()); 
+0

Ok私はクラスパスで間違いを犯していました。とにかく助けてくれてありがとう。 – Leo

1

import static org.junit.Assert.assertArrayEquals; 
import static org.junit.Assert.assertEquals; 
import static org.junit.Assert.assertFalse; 
import static org.junit.Assert.assertTrue; 

それとも単に使用:

import static org.junit.Assert.assertFalse; 
関連する問題