【发布时间】:2021-03-26 10:23:01
【问题描述】:
/*Please help I ma trying from last 2 days but no luck. tried different solution but checkEnvironment method is not returning returning TRUE value . I want method should return value without execution of method .Please ignore compilation error if have. */
enter code here@RunWith(SpringJUnit4Runner.class)
@WebAppConfiguration
@ContextConfiguration(类 = { webconfix.clss })
公共类 LoginControllerTest {
@Autowired
private WebApplicationContext webcontext;
@Mock
LoginServiceImpl serviceimpl;
@InjectMocks
LoginController loginController
private MockMvc mockmvc;
ObjectMapper objMapper = new ObjectMapper();
@Before
@public void Setup() throws Exception{
MockitoAnnotations.initMocks(this);
mockMvc=MockMvcBuilders.standalonSetup(loginControllerTest)
.build;
mockMvc=MockMvcBuilders.WebAppContextSetup(webcontext)
.build;
}
@Test
public void loginTest() throws Exception{
LoginServiceImpl lgservice= spy( new LoginServiceImpl );
Login lg=new Login("test");
String requestData=objMapper.writeValueAsString(login);
Mockito.doReturn(Boolean.TRUE).when(lgservice).checkEnvironment("abc");
// Mockito.when(lgservice.checkEnvironment("abc")).thenReturn(Boolean.TRUE);
MvcResult result=mockMvc.perform(MockMvcBuilders.post("/login").content(requestdata).contentType(MediaType.APPLICATION_JSON).andExpect(status().isOk)).andReturn();
}
}
请帮助我从过去 2 天开始尝试,但没有运气。尝试了不同的解决方案,但 checkEnvironment 方法未返回返回 TRUE 值。我希望方法应该返回值而不执行方法。如果有,请忽略编译错误。
【问题讨论】:
标签: java spring-mvc mockito