3

I am implementing the code that can considering the following junit test:

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

and here is my classe: 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;

    }
}

and an other class: 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;
    }

}

After running the test I got the following error:

error: incompatible types: Pizza cannot be converted to List

List ingredientiMargherita = pizzaMargherita;

So I don't know how to convert an ArrayList into a List just using the operator =, I can not modify the junit test code.

4
  • 1
    I am 99% sure the unit test code has an error in it. If this was an assignment, go back to your professor and ask if they really meant the line "List ingredientiMargherita = pizzaMargherita;" rather than "List ingredientiMargherita = pizzaMargherita.getIngrediente();". Otherwise the only solution is markspace's. Commented May 28, 2015 at 21:39
  • Yes, the test code is rather suspect. I would verify that it's not mistaken. Requiring a value class like Pizza to implement List is kinda weird. Commented May 28, 2015 at 21:42
  • Either that or what OP has tried to re-write to run the program. Commented May 28, 2015 at 21:45
  • no error in the unit test, the solution i found is here below. Commented May 30, 2015 at 14:04

3 Answers 3

3

If you can't modify the assignment, you have to do this:

public class Pizza implements List {
...
}

Or something equivalent, like

public class Pizza extends AbstractList {
...
}
Sign up to request clarification or add additional context in comments.

3 Comments

From a design point of view this is a terrible solution. But it's the only one that could work. +1.
Yeah, I know, it's kinda ugly. I think your comment about the test case likely being a typo is probably worth it for the OP to check.
And no criticism intended, in case you took it that way. Just wanted to make sure that people didn't try doing this where they didn't have to.
1

On below line in your unit test you are assigning Pizza type to a List:

List ingredientiMargherita = pizzaMargherita;

Change it to something like:

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

And add a getIngredienti() to return List in your Pizza class:

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

1 Comment

This also works, but when the OP said he/she couldn't modify the assignment, I assumed a modification to the test code like this was not possible. If it is possible, this is likely the better solution.
1

i found a solution without editing the junit test code, i had to modify my Pizza.java as follows:

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


} 

now the test is passed correctly.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.