在Java中将一个ArrayList分配给一个List

3
我正在实现可以考虑以下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);
     }

这是我的类:Pizza。
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

(该类与IT技术无关)
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;
    }

}

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

错误:不兼容的类型:无法将Pizza转换为List

List ingredientiMargherita = pizzaMargherita;

因此,我不知道如何仅使用操作符 = 将 ArrayList 转换为 List,我不能修改 JUnit 测试代码。


1
我有99%的把握,单元测试代码中存在错误。如果这是一项作业,请回到您的教授那里询问他们是否真的意味着“List ingredientiMargherita = pizzaMargherita;”这一行,而不是“List ingredientiMargherita = pizzaMargherita.getIngrediente();”。否则,唯一的解决方案就是markspace的。 - DJClayworth
是的,测试代码相当可疑。我会验证它是否有误。要求像Pizza这样的值类实现List有点奇怪。 - markspace
要么就是OP尝试重新编写程序来运行它。 - hitz
单元测试没有错误,我找到的解决方案在下面。 - rainman
3个回答

3
如果您不能修改任务,您需要执行以下操作:
public class Pizza implements List {
...
}

或者其他等价的内容,比如:
public class Pizza extends AbstractList {
...
}

从设计角度来看,这是一个糟糕的解决方案。但这是唯一可行的方案。+1。 - DJClayworth
是的,我知道,它有点丑。我认为你关于测试用例可能是打字错误的评论对于原帖作者来说值得检查。 - markspace
没有任何批评的意思,如果你这样理解了的话。只是想确保人们不会在不必要的地方尝试这样做。 - DJClayworth

1
在您的单元测试中,下面一行代码将“Pizza”类型分配给了一个“List”:
List ingredientiMargherita = pizzaMargherita;

将其更改为类似以下内容的东西:
List<Ingrediente> ingredientiMargherita = pizzaMargherita.getIngredienti();

在你的 Pizza 类中添加一个 getIngredienti() 方法来返回 List
public ArrayList<Ingrediente> getIngredienti(){
     return ingredienti;
}

3
这个方法也可以,但是当原帖说他/她无法修改任务时,我认为像这样修改测试代码可能不可行。如果可以修改的话,这可能是更好的解决方案。 - markspace

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


} 

现在测试已经通过正确。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接