使用mockito将实体参数捕获到webtarget.request.put

时间:2017-12-05 06:29:48

标签: java junit mockito

我正在尝试捕获传递给我的webtarget的参数。但IntelliJ一直告诉我这是错误的

你不能捕获Entity.class或者我做错了吗?

代码段

private WebTarget webTarget;

response = target.request("application/hal+json").put(Entity.entity(inputString, MediaType.APPLICATION_JSON_TYPE));

我的测试

@RunWith(MockitoJUnitRunner.class)
public class ConnectorClientTest {

    @Mock
    WebTarget webTarget;

    @Mock
    Invocation.Builder builder;

    @InjectMocks
    ConnectorClient connectorClient;


    @Before
    public void setUp() throws Exception {
        Response response = null;
        when(webTarget.path(any())).thenReturn(webTarget);
        when(webTarget.path(any())).thenReturn(webTarget);
        when(webTarget.request(anyString())).thenReturn(builder);
        when(builder.put(any(Entity.class)))
            .thenReturn(response);
    }

        @Test
        public void initiatePropertyIdentifierSearchTest(){

        PropertyIdentifierSearchInput input = new 
        PropertyIdentifierSearchInput("12345670", BigInteger.valueOf(123));

        connectorClient.initiatePropertyIdentifierSearch(input);
        ArgumentCaptor<Entity> argument = ArgumentCaptor.forClass(Entity.class);
        verify(webTarget.request((String[]) 
        Mockito.any()).put(argument.capture()));
        Entity entity = argument.getValue();
        //assert the entity


    }

1 个答案:

答案 0 :(得分:2)

verify行错了。您必须验证builder

verify(builder).put(argument.capture());