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

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

基于其他方法返回類型的單元測試布爾方法

基于其他方法返回類型的單元測試布爾方法

倚天杖 2023-08-09 15:26:02
單元測試新手,我正在尋找一種對布爾方法進(jìn)行單元測試的方法,該方法已通過其他兩種方法結(jié)果進(jìn)行驗(yàn)證。 protected boolean isUpdateNeeded(){  return (method1() && method2()); }對于本示例,其他方法如下所示。protected boolean method1() {  return false; }protected boolean method2() {  return true; } 但是如果需要的話,這兩種方法可以被覆蓋。我不知道現(xiàn)在這是否真的重要所以我的測試背后的想法是這樣的。找到一種方法將 true/false 傳遞給 method1 或 method2 以滿足所需的可能結(jié)果。@Test public void testCheckToSeeIfUpdateIsNeeded(){    assertTrue('how to check here');    asserFalse('how to check here');   assertIsNull('was null passed?');   }
查看完整描述

2 回答

?
FFIVE

TA貢獻(xiàn)1797條經(jīng)驗(yàn) 獲得超6個(gè)贊

如果另一個(gè)類擴(kuò)展了該類并重寫了 method1 和 method2,則開發(fā)該類的人員有責(zé)任測試更改。


您可以模擬 method1 和 method2,但隨后會將類的結(jié)構(gòu)與測試用例耦合起來,從而使以后更難進(jìn)行更改。


你在這里有責(zé)任測試你班級的行為。我看到有問題的方法被稱為isUpdateNeeded. 那么讓我們測試一下。我將按照我的想象填寫課程。


class Updater {

    Updater(String shouldUpdate, String reallyShouldUpdate) {...}

    boolean method1() { return shouldUpdate.equals("yes"); }

    boolean method2() { return reallyShouldUpdate.equals("yes!"); }

    boolean isUpdateNeeded() { ...}

}


class UpdaterTest {

    @Test

    void testUpdateIsNeededIfShouldUpdateAndReallyShouldUpdate() {

        String shouldUpdate = "yes";

        String reallyShouldUpdate = "yes!"

        assertTrue(new Updater(shouldUpdate, reallyShouldUpdate).isUpdateNeeded());

    }

    .... more test cases .....

}

請注意此測試如何在給定輸入的情況下斷言更新程序的行為,而不是它與某些其他方法的存在的關(guān)系。


如果您希望測試演示重寫方法時(shí)會發(fā)生什么,請?jiān)跍y試中子類化 Updater 并進(jìn)行適當(dāng)?shù)母摹?/p>


查看完整回答
反對 回復(fù) 2023-08-09
?
慕沐林林

TA貢獻(xiàn)2016條經(jīng)驗(yàn) 獲得超9個(gè)贊

所以,例如你有課:


public class BooleanBusiness {


  public boolean mainMethod(){

    return (firstBoolean() && secondBoolean());

  }


  public boolean firstBoolean(){

    return true;

  }


  public boolean secondBoolean() {

    return false;

  }


}

然后你可以編寫這樣的測試:


import static org.junit.Assert.*;

import static org.mockito.Mockito.when;


import org.junit.Test;

import org.mockito.Mockito;


public class BooleanBusinessTest {


  @Test

  public void testFirstOption() {

    BooleanBusiness booleanBusiness = Mockito.spy(BooleanBusiness.class);

    when(booleanBusiness.firstBoolean()).thenReturn(true);

    when(booleanBusiness.secondBoolean()).thenReturn(true);

    assertTrue(booleanBusiness.mainMethod());

  }


  @Test

  public void testSecondOption() {

    BooleanBusiness booleanBusiness = Mockito.spy(BooleanBusiness.class);

    when(booleanBusiness.firstBoolean()).thenReturn(true);

    when(booleanBusiness.secondBoolean()).thenReturn(false);

    assertFalse(booleanBusiness.mainMethod());

  }

}


查看完整回答
反對 回復(fù) 2023-08-09
  • 2 回答
  • 0 關(guān)注
  • 146 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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