I have the class Mappa with a subclass MappaFermi and an attribute mappaName (to choose from an Enum).
I am trying to write a test whith the constructor MappaFermi() but i can't seem to write it correctly.
public class Mappa {
private Name mappaName;
public Mappa (Name mappaName){
this.mappaName=mappaName;
...
}
}
public class MappaFermi extends Mappa {
public MappaFermi(Name mappaName) {
super(mappaName);
}
}
public enum Name {
FERMI, GALILEI, GALVANI
}
I have tried all the suggestions given by eclipse but still get an error.
public class MappaFermiTest {
@Test
public void testMappaFermi() {
Mappa mappa = new MappaFermi(Name.FERMI);
assertNotNull(mappa);
}
@Test
public void testMappaFermi() {
Mappa mappa = new MappaFermi();
assertNotNull(mappa);
}
}
If you want to use
new MappaFermi()
, you have to define a no-argument constructor. This is the default constructor, and any class that has no other constructor will implicitly have a no-arg constructor. But as soon as you define a constructor with arguments, likeMappa(Name mappaName)
, the no-arg default constructor is no longer available and has to be explicitly defined.Also, testing
assertNotNull(mappa);
right afterMappa mappa = new MappaFermi(whatever);
is pretty pointless, as there is no way it could benull
at this point.