mockito when().thenreturn 不适用于使用 Capire 的mockMVC

问题描述 投票:0回答:0
@Slf4j
@SpringBootTest
@AutoConfigureMockMvc
@ActiveProfiles( "test" )
@TestInstance( TestInstance.Lifecycle.PER_CLASS )
class HeadActionTest extends AbstractBoTest {
    @BeforeEach
    @Override
    public void setup( TestInfo testInfo ) throws Exception {
       super.setup( testInfo );
       MockitoAnnotations.initMocks( this );
    }

    @Test
    @WithMockUser( "Wfl_User")
    @Tag( "BTPDestinations")
    void startWFOk( ) throws Exception {
        NameProvider nameProvider1 = Mockito.mock( NameProvider.class );
        Mockito
          .when( nameProvider1.getHttpDestinationName( anyString( ) ) )
          .thenReturn( "11111111111111" );

        MockHttpServletResponse res = mvc
          .perform( MockMvcRequestBuilders
            .post( "url_to_myHandler" )
          .andExpect( status( ).isOk( ) )
          .andReturn( )
          .getResponse( );
         
        ......... and more code ........
       
    }
}

下面是myHandler,请求发送到的地方。

@Component
@ServiceName( Monitor_.CDS_NAME)
@RequiredArgsConstructor
public class MyHandler implements EventHandler {
    @Before
    public void beforeAction( EventContext ctx ) {  
      String test = nameProvider.getHttpDestinationName( "someString" );
        ......... and more code ........
    }
}

问题是当测试运行时,

nameProvider.getHttpDestinationName()

没有返回“111111111”,而是仍然经过了原始码。

但是如果我在没有mockMvc请求的情况下运行代码,它会按预期返回“1111111111”。

任何帮助和提示表示赞赏!

java mockito mockmvc
© www.soinside.com 2019 - 2024. All rights reserved.