Question

I'm quite lost on this one. I have some acceptence tests running with JUnit4 based on Spring. Now I also want to add Unit Tests. To make them fast I skip the context and Inject the Mocks with PowerMock. However all of the sudden reflection wont work any more.

public class TestSomething {
    @Test
    public void nothingWrongWithThis() {
        Class<?> type = Client.class;
        type.getDeclaredMethods();
    }
}

The second line will return null like any other method call to the type except getName()

If I use a context however it will work:

@TransactionConfiguration
@ContextConfiguration({ "classpath:dw-product-context-test.xml" })
@Transactional
@RunWith(SpringJUnit4ClassRunner.class)
public class TestSomething {
    @Test
    public void nothingWrongWithThis() {
        Class<?> type = Client.class;
        type.getDeclaredMethods();
    }
}

What's wrong here? Shouldn't reflection work without any runner or context?

Not even adding the line

private Client client = new Client();

will change anything (Thought maybe the runtime needed to initialize the class in order to be able to reflect upon it)

Oh and adding the

@RunWith(PowerMockRunner.class)

won't change anything either.

Any ideas?

Thanks!

PS: Will be out of town for a day starting now, so I'll read any answer in around 35 hours.


Edit

Just figured out what was going on: I started in my debugger and opened Client.class.declaredMethods which was null. When I run getDeclaredMethods() it will get them however. So it looked as if everything was null at first which confused me but the debugger just didn't run get...() on all fields leaving them null initially

If I use a spring context it will load all beans (Client is an @Entity) and fill all the reflection fields with soft references just as if I called get..() on all of them.

Was it helpful?

Solution

Just figured it out: Missing knowledge about how reflections work when used in a debugger ;) Check my edit... Will mark this as answered but leave it as reference.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top