将ArrayList分配给java中的List

时间:2015-05-28 21:32:57

标签: java list arraylist junit

我正在实现可以考虑以下junit测试的代码:

package it.unica.pr2.pizze.test;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;
import static org.junit.Assert.assertTrue;

import org.junit.Test;
import org.junit.Ignore;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
import java.util.*; 
import it.unica.pr2.pizze.*; 

@RunWith(JUnit4.class)
public class TestPizza {

  @Test
    public void test1() {
      Ingrediente mozzarella = new Ingrediente("mozzarella",50);
      Ingrediente pomodoro = new Ingrediente("pomodoro",10);
      Ingrediente[] ingredienti = new Ingrediente[] {mozzarella, pomodoro}
      Pizza pizzaMargherita = new Pizza(ingredienti);
      assertTrue( pizzaMargherita.calorie() == 60 );
      List ingredientiMargherita = pizzaMargherita;
        assertTrue(ingredientiMargherita.size() ==2);
        assertTrue(ingredientiMargherita.get(0) == mozzarella);
        assertTrue(ingredientiMargherita.get(1) == pomodoro);
     }

这是我的classe:披萨

package it.unica.pr2.pizze;
import java.util.ArrayList;
import java.util.List;

public class Pizza  {


    private ArrayList<Ingrediente> ingredienti;


    public Pizza(Ingrediente[] ing) {

        this.ingredienti = new ArrayList<>();

        int i = 0;
        while (i < ing.length) {

            this.ingredienti.add(ing[i]);
            i++;
        }

    }

    public double calorie(){

        double sumaCalorie = 0;

        for(Ingrediente elem: this.ingredienti)
            sumaCalorie += elem.getCalorie();

        return sumaCalorie;

    }
}

和另一类:Ingrediente

package it.unica.pr2.pizze;


public class Ingrediente  {


    private String nomeIngrediente;
    private double calorie;



    public Ingrediente(String nomeIngrediente, double calorie) throws IngredienteNonValidoException {

        this.nomeIngrediente = nomeIngrediente;
        if (calorie < 0) throw new IngredienteNonValidoException();
        else
            this.calorie = calorie;
    }

    public void setNomeIng(String nomeIngrediente) {
        this.nomeIngrediente = nomeIngrediente;
    }

    public void setCalorie(double calorie) {

        this.calorie = calorie;
    }

    public String getNomeIng() {

        return this.nomeIngrediente;
    }

    public double getCalorie() {
        return this.calorie;
    }

}

运行测试后,我收到以下错误:

error: incompatible types: Pizza cannot be converted to List

List ingredientiMargherita = pizzaMargherita;

所以我不知道如何使用operator =将ArrayList转换为List,我无法修改junit测试代码。

3 个答案:

答案 0 :(得分:3)

如果您无法修改作业,则必须执行以下操作:

  mCurConfiguration={0 1.0 ?mcc?mnc en_US ldltr sw800dp w1280dp h775dp 160dpi xlrg land finger -keyb/v/h -nav/h s.88}

或类似的东西,比如

public class Pizza implements List {
...
}

答案 1 :(得分:1)

在单元测试的下方,您要将Pizza类型分配给List

List ingredientiMargherita = pizzaMargherita;

将其更改为:

List<Ingrediente> ingredientiMargherita = pizzaMargherita.getIngredienti();

getIngredienti()课程中添加List以返回Pizza

public ArrayList<Ingrediente> getIngredienti(){
     return ingredienti;
}

答案 2 :(得分:1)

我找到了一个没有编辑junit测试代码的解决方案,我不得不修改我的Pizza.java如下:

package it.unica.pr2.pizze;
import java.util.ArrayList;
import java.util.*;

public class Pizza extends ArrayList   {

    public Pizza(Ingrediente[] ing) {

        for(Ingrediente elem: ing) {

            this.add(elem);
        }

    }

    public int calorie() {
        int calorie = 0;
        for(Object i : this) {
            calorie += ((Ingrediente)i).getCalorie();
        }
        return calorie;
    }


} 

现在测试正确传递。