导入org.junit.Assert时出错

时间:2017-04-09 10:38:08

标签: java unit-testing junit

我的教授给了我一个单元测试的问题。编译后,我收到以下错误:
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;

我已经下载了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());
      }

    }

3 个答案:

答案 0 :(得分:4)

假设您在类路径中有JUnit dependency,请使用import static作为断言方法:

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.*;

答案 1 :(得分:2)

您应该添加关键字static以导入它。一个例子:

 import static org.junit.Assert.assertFalse;

答案 2 :(得分:2)

您要找的是静态导入

using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Data.SqlClient; namespace ComputingProjectwh.TestPages._1._Further_Mechanics { public partial class Moments_and_Energy_Test1 : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { } protected void Submit_Click(object sender, EventArgs e) { if (!this.IsValid) return; int score = 0; List<RadioButtonList> list = new List<RadioButtonList>() { RadioButtonList1, RadioButtonList2, RadioButtonList3, RadioButtonList4, RadioButtonList5, RadioButtonList6, RadioButtonList7, RadioButtonList8, RadioButtonList9, RadioButtonList10 }; foreach (var element in list) { if (element.SelectedValue == "Correct") { score++; } } Response.Write("you scored: " + score); Button1.Visible = false; if (score != 0); { SqlConnection sqlConnection1 = new SqlConnection (@"Data Source=(LocalDb)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\aspnet-ComputingProjectwh-20170404101246.mdf;InitialCatalog=aspnet-ComputingProjectwh-20170404101246;IntegratedSecurity=True"); System.Data.SqlClient.SqlCommand cmd = new System.Data.SqlClient.SqlCommand(); cmd.CommandType = System.Data.CommandType.Text; cmd.CommandText = "INSERT AspNetUserTestScores (Id, MomentAndEnergyTestScore) VALUES (Id, score)"; cmd.Connection = sqlConnection1; sqlConnection1.Open(); cmd.ExecuteNonQuery(); sqlConnection1.Close(); } } } } 行引用了import org.junit.Assert.assertArrayEquals;

中的方法assertArrayEquals

使用静态导入行导入静态方法,使其可以像org.junit.Assert一样调用。试试以下内容:

assertEquals(0,orderedArray.size());

或者你可以:

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;