Java Junit Failure For an Attribute Class

46 Views Asked by At

I have the class Mappa that has mappaName as an attribute (to choose from the enum Name) and a method getMappaName that returns it, when i run a test to compare one of the mappaName chosen from Name i get expected FERMI but was:null

    public class Mappa {
        private Name mappaName;
        private final Settore [][] settore;
        private int Matrice [][];
        private static final int X=23;
        private static final int Y=14;
        public Mappa (Name mappaName){//If i run new Mappa (Name.FERMI)
//isn't supposed to set mappaName to Name.FERMI?
            settore = new Settore[X][Y];
            for (int i=0; i < X; i++){
                for (int j=0; j<Y; j++) {
                    settore[i][j] = new Settore (i,j);
                }
            }
            Matrice = new int[23][14];
            if(mappaName==Name.FERMI){
                settore[10][8]=new Alieni(10,8);
                settore[10][9]=new Umani(10,9);
            }
            if(mappaName==Name.GALILEI||mappaName==Name.GALVANI){
                settore[10][5]=new Alieni(10,5);
                settore[10][7]=new Umani(10,7);
            }
        }

        public Name getMappaName() {
            return mappaName;
        }
    }
    public enum Name {
    FERMI, GALILEI, GALVANI
    }
    @Test
        public void testMappaNome(){
            Mappa mappa = new Mappa(Name.FERMI);
            assertEquals(Name.FERMI, mappa.getMappaName());
        }
2

There are 2 best solutions below

0
On BEST ANSWER

You forgot to assign the local variable mappaName to the field mappaName. Add at the constructor beginning:

this.mappaName = mappaName;
0
On

Inside your constructor, you still have to set the argument mappaName to the class field mappaName. Like below:

public Mappa (Name mappaName){//If i run new Mappa (Name.FERMI)
//isn't supposed to set mappaName to Name.FERMI?
this.mappName = mappaName; // assign the value to your class entry.