【问题标题】:How I can write mockito junit for Listener class我如何为 Listener 类编写 mockito junit
【发布时间】:2015-05-05 14:18:23
【问题描述】:

嗨,如何编写 mockitJunit。

请帮助如何为上述课程编写 mockito junit

【问题讨论】:

  • 谷歌搜索“mockito spring”(因为这似乎是您正在使用的)将是一个很好的起点;如果您不费吹灰之力就来提出问题,如果您的问题被解决,请不要感到惊讶。
  • 正如@fge 所说,去做一些研究,而不是仅仅寻求解决方案而不用谷歌搜索。

标签: java spring junit mocking mockito


【解决方案1】:

这行得通。只需为您设置的系统属性添加检查即可。

@RunWith(MockitoJUnitRunner.class)
public class AppStartUpContextListenerTest {

    public AppStartUpContextListenerTest() {
    }

    @Mock
    ServletContextEvent mockEvent;
    @Mock
    ServletContext mockServletContext;
    @Mock
    Configuration mockConfig;
    @Mock
    WebApplicationContext mockWebContext;

    /**
     * Test of contextInitialized method, of class AppStartUpContextListener.
     */
    @Test
    public void testContextInitialized() {
        System.out.println("testContextInitialized");

        when(mockEvent.getServletContext()).thenReturn(mockServletContext);

        when(mockServletContext.getAttribute(Matchers.anyString())).thenReturn(mockWebContext);

        AppStartUpContextListener instance = new AppStartUpContextListener();
        instance.contextInitialized(mockEvent);
        // TODO review the generated test code and remove the default call to fail.

        verify(mockEvent, times(1)).getServletContext();

    }

}

【讨论】:

    猜你喜欢
    • 2018-05-15
    • 2018-03-04
    • 2015-08-30
    • 2015-03-08
    • 1970-01-01
    • 2019-06-21
    • 2020-02-27
    • 2017-09-06
    • 1970-01-01
    相关资源
    最近更新 更多