vendredi 10 avril 2020

getClass returns the test class rather than the expected inheritance in JUnit

I have a junit test, ModelTest.java, to run all test cases of the class Model. The model class can be inherited to be, say, BookModel, CarModel, AnimalModel, etc.

In the Model class, the getClass is extensively used to get current class, say, BookModel, CarModel, AnimalModel. Sadly, I found these getClass calls returns the JUnit Class name: ModelTest. Code looks like somthing bellow,

ModelTest.Java

public class ModelTest {
    @Test
    public void all() {
        Model model=new Model();
        model.save();
   }
}

Model.java

public class Model {
   public void save() {
       Class c=getClass();
       //getClass returns the ModelTestClass(X); it is the "Model" class expected to be returned
    }
}

This only happens in junit env. When testing by a self-made main, it works normally. Any ideas to resolve this issue?





Aucun commentaire:

Enregistrer un commentaire