java Jmock 如何与 HttpSession 和 HttpServletRequest 一起使用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/230389/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 11:32:09  来源:igfitidea点击:

How is Jmock used with HttpSession and HttpServletRequest

javajunitmockingjmock

提问by SWD

I am new to jmock and trying to mock an HttpSession. I am getting:

我是 jmock 的新手并试图模拟 HttpSession。我正进入(状态:

java.lang.AssertionError: unexpected invocation: httpServletRequest.getSession() no expectations specified: did you... - forget to start an expectation with a cardinality clause? - call a mocked method to specify the parameter of an expectation?

java.lang.AssertionError:意外调用:httpServletRequest.getSession() 没有指定期望:你... - 忘记用基数子句开始期望吗?- 调用一个模拟方法来指定期望的参数?

the test method:

测试方法:

@Test

@测试

public void testDoAuthorization(){

    final HttpServletRequest request = context.mock(HttpServletRequest.class);
    final HttpSession session = request.getSession();

    context.checking(new Expectations(){{
       one(request).getSession(true); will(returnValue(session));
   }});

    assertTrue(dwnLoadCel.doAuthorization(session));
}

I have done a bit of searching and it isn't clear to me still how this is done. Feels like I am missing some small piece. Anyone with experience in this can just point me in the right direction. thanks

我已经做了一些搜索,但我仍然不清楚这是如何完成的。感觉就像我错过了一些小片段。任何有这方面经验的人都可以为我指明正确的方向。谢谢

回答by Kris Pruden

You don't need to mock the request object. Since the method you're testing (dwnLoadCel.doAuthorization()) only depends on an HttpSessionobject, that is what you should mock. So your code would look like this:

您不需要模拟请求对象。由于您正在测试的方法 ( dwnLoadCel.doAuthorization()) 仅依赖于一个HttpSession对象,因此您应该模拟它。所以你的代码看起来像这样:

public void testDoAuthorization(){
    final HttpSession session = context.mock(HttpSession.class);

    context.checking(new Expectations(){{
        // ???
    }});

    assertTrue(dwnLoadCel.doAuthorization(session));

}

}

The question becomes: what do you expect the SUT to actually dowith the session object? You need to express in your expectations the calls to sessionand their corresponding return values that are supposed to result in doAuthorizationreturning true.

问题变成了:您希望 SUT 实际使用会话对象什么?您需要在您的期望中表达对session应该导致doAuthorization返回的调用及其相应的返回值true

回答by matt b

I think you need to tell the JMock context how many times you expect the method to be called before you actually go ahead and call it.

我认为您需要告诉 JMock 上下文您希望在实际继续调用该方法之前调用该方法的次数。

final HttpServletRequest request = context.mock(HttpServletRequest.class);

context.checking(new Expectations(){{
  one(request).getSession(true); will(returnValue(session));
}});

final HttpSession session = request.getSession();

I'm not super familiar with JMock but do you actually care in your dwnLoadCelunit test how many times certain methods in the mocked object are called? Or are you just trying to test your class that depends on a HttpSession without an actual Session? If it's the latter than I think that JMock is overkill for you.

我对 JMock 不是很熟悉,但是你真的关心你的dwnLoadCel单元测试中被模拟对象中的某些方法被调用了多少次吗?或者您只是想测试依赖于 HttpSession 而没有实际会话的类?如果是后者,我认为 JMock 对你来说有点过分了。

You might want to look into either creating a class that implements the HttpSessioninterface yourself for the purposes of unit testing only (a stub), and running your tests off of that, or you should take a look at dwnLoadCeland determine if it reallyneeds to have a reference to the HttpSession, or if it just needs some properties within the HttpSession. Refactor dwnLoadCelto only depend on what it actually needs (a Mapor a certain parameter value within the Session object) - this will make your unit test easier (the dependency on the servlet container goes bye-bye).

您可能想要研究创建一个HttpSession自己实现接口的类,仅用于单元测试(存根),然后运行您的测试,或者您应该查看dwnLoadCel并确定它是否真的需要对 HttpSession 的引用,或者它是否只需要 HttpSession 中的某些属性。重构dwnLoadCel为仅依赖于它实际需要的内容(MapSession 对象中的一个或某个参数值)——这将使您的单元测试更容易(对 servlet 容器的依赖消失了)。

I think that you have some level of dependency injection in your class being tested already, but you might be dependent on too broad of an object. The Google Test Bloghas had a lotof excellentarticleson DI lately that you might find useful (I sure have).

我认为你已经在你的类中测试了某种程度的依赖注入,但你可能依赖于太广泛的对象。谷歌测试博客最近有很多关于 DI的优秀文章,你可能会觉得有用(我肯定有)。