我如何为Listener类编写mockito junit

时间:2015-05-05 14:18:23

标签: java spring junit mocking mockito

你好怎么写mockitJunit。

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

1 个答案:

答案 0 :(得分:2)

这很有用。只需添加对您设置的系统属性的检查。

@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();

    }

}