mercredi 18 août 2021

Get constructor for method local-class by reflection in JUnit4 test

I need Constructor<TestClass> instance for local class TestClass in my JUnit4 test method.

public void testMethod() throws NoSuchMethodException {
    class TestClass {
    }
    
    Constructor<TestClass> constructor = TestClass.class.getDeclaredConstructor();

    // dos sth in test
}

When I'm trying get constructor using getDeclaredConstructor() I'm getting the error NoSuchMethodException().

I tried to run the same sideways logic in my IDE scratch and the constructor is found. The difference is that there TestClass is declared in the STATIC method.

My question is why this problem occurs and how does it relate to the static / non-static method, and how can I get around this problem in my test method.





Aucun commentaire:

Enregistrer un commentaire