【问题标题】:How to return value from method without executing code using mockito如何在不使用 mockito 执行代码的情况下从方法返回值
【发布时间】: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


    【解决方案1】:
     @Mock
             LoginServiceImpl serviceimpl;
    

    检查登录控制器声明。是否有任何类型的bean:

    登录服务实现

    如果我错了,请纠正我,你在控制器中有接口声明吗?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-03-21
      • 1970-01-01
      • 1970-01-01
      • 2021-05-28
      相关资源
      最近更新 更多