У меня есть следующее поле в тестируемом классе:
class ClassUnderTest {
@Context
public javax.ws.rs.core.SecurityContext securityContext;
public void someMethod() {
securityContext.getUserPrinciple(); // securityContext is always null here - never mocked
}
}
Но я не могу заставить JMockit издеваться над ним, используя ожидания или подход MockUp, в обоих случаях в тестируемом классе securityContext всегда равен нулю?
@Test
public void usingExpectations() {
new Expectations() {
@Mocked
SecurityContext mockSecurityContext;
@Mocked
Principal mockPrinciple;
{
mockSecurityContext.getUserPrincipal();
result = mockPrinciple;
mockPrinciple.getName();
result = username;
}
};
new ClassUnderTest().someMethod();
}
@Test
public using usingMockUp() {
new MockUp<SecurityContext>() {
@Mock
Principle getUserPrincipal() {
// return something
};
}
new ClassUnderTest().someMethod();
}