mock contextloader.getcurrentwebapplicationcontext()我该怎么做?

ilmyapht  于 2021-07-05  发布在  Java
关注(0)|答案(2)|浏览(512)

我试图在使用mockito时模拟contextloader.getcurrentwebapplicationcontext()调用,但它无法模拟。

  1. //here is my source code
  2. @Mock
  3. org.springframework.web.context.WebApplicationContext webApplicationContext;
  4. //test Case Body
  5. try (MockedStatic<ContextLoader> dummy = Mockito.mockStatic(ContextLoader.class)) {
  6. AnswerInfo answerInfo = Mockito.mock(AnswerInfo.class);
  7. TranDescScoreInfo descScoreInfo2 = Mockito.mock(TranDescScoreInfo.class);
  8. when(ctx.getBean("answerInfo")).thenReturn(answerInfo);
  9. when(ctx.getBean("tranDescScoreInfo")).thenReturn(descScoreInfo2);
  10. dummy.when(() -> ContextLoader.getCurrentWebApplicationContext()).thenReturn(webApplicationContext);
  11. //ContextLoader.getCurrentWebApplicationContext() getting null I dont why it getting null.
  12. }
  13. //Below Code is my business logic
  14. ApplicationContext ctx = ContextLoader.getCurrentWebApplicationContext();
  15. AnswerInfo answerInfo = (AnswerInfo) ctx.getBean("answerInfo");
  16. tranDescScoreInfo = (TranDescScoreInfo) ctx.getBean("tranDescScoreInfo");

//getbean获取null是因为我没有得到预期的模拟调用注意:我不想更改我的业务逻辑

f3temu5u

f3temu5u1#

你必须把代码移到try里面。我希望这对你有用:

  1. class UserTest {
  2. @Mock
  3. WebApplicationContext webApplicationContext;
  4. @BeforeEach
  5. void setUp() throws Exception {
  6. MockitoAnnotations.openMocks(this);
  7. }
  8. @Test
  9. void test() {
  10. //test Case Body
  11. try (MockedStatic<ContextLoader> dummy = Mockito.mockStatic(ContextLoader.class)) {
  12. Mockito.when(webApplicationContext.getBean("answerInfo")).thenReturn(new String());
  13. dummy.when(ContextLoader::getCurrentWebApplicationContext).thenReturn(webApplicationContext);
  14. //Below Code is my business logic
  15. ApplicationContext ctx = ContextLoader.getCurrentWebApplicationContext();
  16. assertNotNull( ctx.getBean("answerInfo"));
  17. }
  18. }
  19. }
展开查看全部
cgyqldqp

cgyqldqp2#

  1. I tried this after some RnD it's working for me
  2. The below code is worked for me:
  3. @Mock
  4. static ServletContext servletContext;
  5. @Mock
  6. ContextLoader contextLoader;
  7. @Mock
  8. org.springframework.web.context.WebApplicationContext webApplicationContext;
  9. @Mock
  10. HttpServletRequest request;
  11. private AutoCloseable closeable;
  12. @BeforeAll
  13. static void setUpBeforeClass() throws Exception {
  14. MockServletContext msc = new MockServletContext();
  15. msc.addInitParameter(ContextLoader.CONFIG_LOCATION_PARAM, "/test/config/spring/SpringBeansTest.xml");
  16. ServletContextListener listener = new ContextLoaderListener();
  17. ServletContextEvent event = new ServletContextEvent(msc);
  18. listener.contextInitialized(event);
  19. }
  20. @AfterAll
  21. static void tearDownAfterClass() throws Exception {
  22. }
  23. @BeforeEach
  24. void setUp() throws Exception {
  25. closeable = MockitoAnnotations.openMocks(this);
  26. Mockito.doReturn(Random.class).when(ctx).getBean(Mockito.anyString());
  27. RequestContextHolder.setRequestAttributes(new ServletRequestAttributes(request));
  28. }
  29. @AfterEach
  30. void tearDown() throws Exception {
  31. closeable.close();
  32. closeable = null;
  33. }
  34. //test case body
  35. try (MockedStatic<ContextLoader> dummy = Mockito.mockStatic(ContextLoader.class)) {
  36. dummy.when(() -> ContextLoader.getCurrentWebApplicationContext()).thenReturn(webApplicationContext);
  37. }
展开查看全部

相关问题