第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

模擬 void 方法返回空指針異常

模擬 void 方法返回空指針異常

牧羊人nacy 2023-06-08 14:37:58
我在嘗試對函數(shù)調(diào)用進行單元測試時遇到問題。messageProducer.sendMessage()即使已存根,調(diào)用也會因 void 方法調(diào)用而失敗。請在下面找到我的代碼的簡化快照。我正在使用 doAnswer() 存根來模擬 void 方法(基于 StackOverflow 上的早期答案)。我什至嘗試了其他選項doThrow()和doNothing()存根,但在調(diào)用存根方法時它們也會因相同的 NPE 而失敗 :(。感謝有人可以提出解決方案/解決方法。非常感謝。測試類// Test class@RunWith(MockitoJUnitRunner.class)public class RetriggerRequestTest {    @Mock    private MessageProducer messageProducer;     @InjectMocks    private MigrationRequestServiceImpl migrationRequestService;     @Before    public void init() {        MockitoAnnotations.initMocks(this);    }    @Test    public void sendRetriggerRequest() throws Exception {        // Below two stubbings also not Work, NPE encountered!        //doNothing().when(messageProducer).sendMessage(any(), anyLong());        //doThrow(new Exception()).doNothing().when(messageProducer).sendMessage(any(), anyLong());        doAnswer(new Answer<Void>() {            public Void answer(InvocationOnMock invocation) {                Object[] args = invocation.getArguments();                System.out.println("called with arguments: " + Arrays.toString(args));                return null;            }        }).when(messageProducer).sendMessage(any(EMSEvent.class), anyLong());        try {            // Gets Null pointer exception            migrationRequestService.retriggerRequest(emsRetriggerRequest);        }        catch (Exception ex) {            fail(ex.getMessage());        }    }
查看完整描述

2 回答

?
夢里花落0921

TA貢獻1772條經(jīng)驗 獲得超6個贊

如果您只是想捕獲參數(shù)并以某種方式處理/驗證它們,請不要使用 doAnswer。Mockito 有一個定義的功能,稱為ArgumentCaptor專為此而設(shè)計。通過使用它,您將不需要像您那樣與 void 方法討價還價:


@Mock private MessageProducer messageProducer;


@Captor private ArgumentCaptor<Event> eventCaptor;

@Captor private ArgumentCaptor<Long> longCaptor;


@InjectMocks

private MigrationRequestServiceImpl migrationRequestService;


@Test

public void sendRetriggerRequest() throws Exception {

   // When

   migrationRequestService.retriggerRequest(emsRetriggerRequest);


   // Then

   verify(messageProducer).sendMessage(eventCaptor.capture(), longCaptor.capture());


   Event e = eventCaptor().getValue();

   Long l = longCaptor().getValue();

}


查看完整回答
反對 回復(fù) 2023-06-08
?
胡子哥哥

TA貢獻1825條經(jīng)驗 獲得超6個贊

實際上我不想對參數(shù)做任何事情,我只需要跳過這個方法調(diào)用。我只是將 doAnswer 與一些偽代碼一起使用,因為 doNothing() 或 doThrow() 不適用于此方法。


但是我能夠解決這個問題。被注入 Mocks (MigrationRequestServiceImpl) 的類的自動裝配組件 (eventsConfigProperties) 之一沒有在測試類中被模擬!感謝@daniu 指出這一點。


來自 Mockito 的堆棧跟蹤對調(diào)試問題不是很有幫助,它只是在方法調(diào)用時給出了一個空指針異常,這讓我認為可能還有其他問題!


為這個錯誤道歉,我的錯,但謝謝你,很高興知道 ArgumentCaptor,未來測試可能需要它!


必須添加這個自動連接到 MigrationRequestService 類中的條目。


// Test class

@RunWith(MockitoJUnitRunner.class)

public class RetriggerRequestTest {

    @Autowired

    EventsConfigProperties eventsConfigProperties;


    // Other declarations

}


查看完整回答
反對 回復(fù) 2023-06-08
  • 2 回答
  • 0 關(guān)注
  • 212 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號