我正在测试一个类。这个类调用一个服务(1)

我正在测试一个类。这个类调用一个服务(假设它叫client,我们想叫client.put())

put() 应该返回一个响应,但至少在测试中,响应是 null

我不知道我是否只是没有正确设置模拟并想在这里与你们进行理智检查

public class ATest {
    @Mock
    private ServiceProto.PutItemsResponse res;
    ...(private variables)...
    @Before
    public void setUp() throws Exception {

图片[1]-我正在测试一个类。这个类调用一个服务(1)-唐朝资源网

client = mock(Client.class); clientFactory = mock(ClientFactory.class); when(clientFactory.get(any())).thenReturn(client); ...(initializing private vars for constructor as mock variables, example below...) captionConverter = mock(CaptionToCTItemConverter.class); when(privateVar.convert(any(obj.class))).thenReturn(Item.newBuilder().build()); classAToTest = spy(new ClassAToTest(private variables); } @Test public void putItem() { long id = 4710582L; AObject aObject = testUtils.getObject(); doReturn(res).when(client).putItems(any(ServiceProto.PutItemsRequest.class)); System.out.println("result is "+ res); try { classAToTest.putMethod(aObject); } catch (NullPointerException e) { } verify(creativeToolsClient, Mockito.times(1)).putItems(any(IngestionServiceProto.PutItemsRequest.class)); } }

这是正在测试的方法

public void putMethod(AObject aObject) {

图片[2]-我正在测试一个类。这个类调用一个服务(1)-唐朝资源网

final String id = Long.toString(aObject.getId()); ServiceProto.PutItemsResponse putItemsResponse = null; Exception putItemsFailure = null; putItemsResponse = client.putItems(ServiceProto.PutItemsRequest.newBuilder() .putItems( id, ServiceProto.PutItemsRequest.Item.newBuilder()).build()) .build()); if (putItemsResponse == null) { logger.warning("PutItems request has failed: "+ (putItemsFailure == null ? "null" : putItemsFailure.getMessage())); } }

当我运行它时它会发出警告

putItems 方法适用于其他人。是不是我设置的 mock 不正确?

© 版权声明
THE END
喜欢就支持一下吧
点赞239赞赏 分享
评论 抢沙发
头像
欢迎您留下宝贵的见解!
提交
头像

昵称

取消
昵称表情代码图片

    暂无评论内容