我仍在学习JMockit,需要帮助来理解它.
I am still learning JMockit and need help understanding it.
我正在测试使用超类方法的类.当我的测试尝试使用超类方法时,由于其中的代码使用struts操作上下文获取会话并从会话中提取对象,因此我的测试得到了一个空指针.
I am testing a class that uses superclass methods. My test gets a null pointer when it attempts to use the superclass method due to code inside it that uses struts action context to get the session and pull an object from the session.
我想绕过受保护方法内部的struts会话内容的方法.
The method I want to bypass the struts session stuff inside the protected method.
public class MyExtendingClass extends MySuperClass{ public void methodIamTesting(){///} } public abstract class MySuperClass{ //I want to mock this method protected Object myProtectedSuperClassMethod(){ // struts action context code that returns an object//} }测试代码
@Test public void testRunsAndDoesntPass() { Mockit.setUpMock(MySuperClass.class, new MySuperClass(){ public Object myProtectedSuperClassMethod() { return object; } }); // real class method invocation happens assertEquals(expected, actual);}
我一直在获得NullPointers,就像没有模拟游戏一样 不知道下一步该怎么做.我读过的所有文档和代码示例都说要在setUpMock中将超类方法声明为公共方法,它应该可以工作.
I keep getting NullPointers just like if I didn't have the mock Not sure what to try next. All the docs and code samples I have read say to just declare the superclass method as public in the setUpMock and it should work.
我不能嘲笑整个课程,因为那是我正在测试的课程.
I can't mock the entire class because that is the class I am testing.
推荐答案您完全将父类实现屏蔽了@Mocked final MySuperClass base
you mask the parent class implementation out totally @Mocked final MySuperClass base
abstract class MySuperClass{ protected Object myProtectedSuperClassMethod(){ } class MyExtendingClass extends MySuperClass{ public void methodIamTesting(){///} } @Test public void testRunsAndDoesntPass(@Mocked final MySuperClass base ) { //you could mask out all the base class implementation like this new Expectations(){{ invoke(base, "myProtectedSuperClassMethod"); }}; // real class method invocation happens // ... assertEquals(expected, actual); }更多推荐
超类中的JMockit模拟保护方法和真实子类中的静态测试方法
发布评论