如何解决在Mockito中模拟使用ContextLoader.getCurrentWebApplicationContext调用我该怎么做?
我尝试在使用Mockito时模拟ContextLoader.getCurrentWebApplicationContext()调用,但模拟失败。
//here is my source code
@Mock
org.springframework.web.context.WebApplicationContext webApplicationContext;
//test Case Body
try (MockedStatic<ContextLoader> dummy = Mockito.mockStatic(ContextLoader.class)) {
AnswerInfo answerInfo = Mockito.mock(AnswerInfo.class);
TranDescScoreInfo descScoreInfo2 = Mockito.mock(TranDescScoreInfo.class);
when(ctx.getBean("answerInfo")).thenReturn(answerInfo);
when(ctx.getBean("tranDescScoreInfo")).thenReturn(descScoreInfo2);
dummy.when(() -> ContextLoader.getCurrentWebApplicationContext()).thenReturn(webApplicationContext);
//ContextLoader.getCurrentWebApplicationContext() getting null I dont why it getting null.
}
//Below Code is my business logic
ApplicationContext ctx = ContextLoader.getCurrentWebApplicationContext();
AnswerInfo answerInfo = (AnswerInfo) ctx.getBean("answerInfo");
tranDescScoreInfo = (TranDescScoreInfo) ctx.getBean("tranDescScoreInfo");
// ctx.getBean获得空值,因为我没有按预期在此处进行模拟调用 注意:我不想更改我的业务逻辑
解决方法
您必须在尝试中移动代码。我希望这对您有用:
class UserTest {
@Mock
WebApplicationContext webApplicationContext;
@BeforeEach
void setUp() throws Exception {
MockitoAnnotations.openMocks(this);
}
@Test
void test() {
//test Case Body
try (MockedStatic<ContextLoader> dummy = Mockito.mockStatic(ContextLoader.class)) {
Mockito.when(webApplicationContext.getBean("answerInfo")).thenReturn(new String());
dummy.when(ContextLoader::getCurrentWebApplicationContext).thenReturn(webApplicationContext);
//Below Code is my business logic
ApplicationContext ctx = ContextLoader.getCurrentWebApplicationContext();
assertNotNull( ctx.getBean("answerInfo"));
}
}
}
,
I tried this after some RnD it's working for me
The below code is worked for me:
@Mock
static ServletContext servletContext;
@Mock
ContextLoader contextLoader;
@Mock
org.springframework.web.context.WebApplicationContext webApplicationContext;
@Mock
HttpServletRequest request;
private AutoCloseable closeable;
@BeforeAll
static void setUpBeforeClass() throws Exception {
MockServletContext msc = new MockServletContext();
msc.addInitParameter(ContextLoader.CONFIG_LOCATION_PARAM,"/test/config/spring/SpringBeansTest.xml");
ServletContextListener listener = new ContextLoaderListener();
ServletContextEvent event = new ServletContextEvent(msc);
listener.contextInitialized(event);
}
@AfterAll
static void tearDownAfterClass() throws Exception {
}
@BeforeEach
void setUp() throws Exception {
closeable = MockitoAnnotations.openMocks(this);
Mockito.doReturn(Random.class).when(ctx).getBean(Mockito.anyString());
RequestContextHolder.setRequestAttributes(new ServletRequestAttributes(request));
}
@AfterEach
void tearDown() throws Exception {
closeable.close();
closeable = null;
}
//test case body
try (MockedStatic<ContextLoader> dummy = Mockito.mockStatic(ContextLoader.class)) {
dummy.when(() -> ContextLoader.getCurrentWebApplicationContext()).thenReturn(webApplicationContext);
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。