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

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

針對(duì)兩個(gè)不同的注入依賴項(xiàng)運(yùn)行單元測(cè)試

針對(duì)兩個(gè)不同的注入依賴項(xiàng)運(yùn)行單元測(cè)試

寶慕林4294392 2021-08-04 17:14:20
我的應(yīng)用程序中有兩個(gè)服務(wù) bean,它們都實(shí)現(xiàn)了一個(gè)接口。對(duì)于該接口,所有方法都必須執(zhí)行相同的操作(但內(nèi)部結(jié)構(gòu)不同)。所以我想編寫一組針對(duì)這兩種服務(wù)運(yùn)行的測(cè)試。(不想寫重復(fù)的代碼)構(gòu)建我的測(cè)試以完成此任務(wù)的最佳方法是什么?(我標(biāo)記了 junit4,因?yàn)檫@是我目前受限的版本。)
查看完整描述

3 回答

?
婷婷同學(xué)_

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

這里的其他答案都很棒,但并不完全符合我的喜好。我最終結(jié)合了

  • JUnit @RunWith(@Parameterized)(運(yùn)行兩個(gè)服務(wù)的測(cè)試)

  • Spring 的新規(guī)則(保持@RunWith(SpringRunner.class)容器、appcontext、webmvc 和注入功能)

以下是片段:

@RunWith(Parameterized.class)


...


@Parameters(name = "Cached = {0}")

public static Boolean[] data() {

    return new Boolean[] { Boolean.TRUE, Boolean.FALSE };

}


@ClassRule

public static final SpringClassRule springClassRule = new SpringClassRule();


@Rule

public final SpringMethodRule springMethodRule = new SpringMethodRule();


@Before

public void setUp() {

     // logic to choose the injected service based on the true/false param

}


查看完整回答
反對(duì) 回復(fù) 2021-08-04
?
慕容3067478

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

幾個(gè)選項(xiàng):

  1. 您可以將所有類存儲(chǔ)在一個(gè)常量中,例如List<Class<? extends YourInterface>> classes = Arrays.asList(Implementation.class)遍歷這些類并為每個(gè)類調(diào)用該方法

  2. 您可以使用反射來查找實(shí)現(xiàn)特定接口的所有類并為每個(gè)類循環(huán)。


查看完整回答
反對(duì) 回復(fù) 2021-08-04
?
慕無忌1623718

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

您可以創(chuàng)建一個(gè) bean 來保存 JavaConfig 中的實(shí)現(xiàn)列表:


public class TestConfig {


    @Bean

    MyService myService1(){

        return new MyService1();

    }


    @Bean

    MyService myService2(){

        return new MyService2();

    }


    @Bean

    public List<MyService> myServices(MyService myService1, MyService myService2){

        List<MyService> allServices = new ArrayList<>();

        allServices.add(myService1);

        allServices.add(myService2);

        return allServices;

    }

}

并最終在您的測(cè)試中迭代此列表:


@RunWith(SpringJUnit4ClassRunner.class)

@ContextConfiguration(classes=TestConfig.class)

public class ServicesTest {

    @Autowired

    private List<MyService> allServices;

    @Test

    public void testAllServices(){    

        for (MyService service : allServices) {

            // Test service here

        }    

    }

}


查看完整回答
反對(duì) 回復(fù) 2021-08-04
  • 3 回答
  • 0 關(guān)注
  • 169 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號(hào)

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