超类中的JMockit模拟保护方法和真实子类中的静态测试方法

编程入门 行业动态 更新时间:2024-10-25 22:27:27
本文介绍了超类中的JMockit模拟保护方法和真实子类中的静态测试方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我仍在学习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模拟保护方法和真实子类中的静态测试方法

本文发布于:2023-10-31 04:02:09,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1544939.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:类中   方法   静态   真实   测试

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!