我正在嘗試為我的服務(wù)編寫(xiě)一個(gè)測(cè)試,該測(cè)試與另一個(gè)從數(shù)據(jù)庫(kù)返回項(xiàng)目的服務(wù)建立連接。我的問(wèn)題是我在測(cè)試中設(shè)置了連接屬性并啟動(dòng)了服務(wù)。這怎么可能是模擬或類(lèi)似的?我的啟動(dòng)服務(wù)方法:public void doStartService() { super.doStartService(); PoolingHttpClientConnectionManager manager = new PoolingHttpClientConnectionManager(); manager.setDefaultMaxPerRoute(maxConnectionsPerRoute); manager.setMaxTotal(maxConnections); RequestConfig requestConfig = RequestConfig.custom() .setConnectTimeout(connectTimeout) .setSocketTimeout(socketTimeout) .setRedirectsEnabled(false).build(); HttpClientBuilder builder = HttpClientBuilder.create(); builder.setDefaultRequestConfig(requestConfig); builder.setConnectionManager(manager); client = builder.build(); }我的設(shè)置測(cè)試方法和一種測(cè)試方法:private ProductCatalogIntegrationService service; @Before public void setup() { service = new Service(); service.setConnectTimeout(10000); service.setSocketTimeout(10000); service.setMaxConnections(10); service.setMaxConnectionsPerRoute(10); service.setUrl("http://localhost:8888/products"); service.doStartService(); } @Test public void testReturnProductById() { service.setProductById(GET_PRODUCT_BY_ID); // set url from get product by id, by this url my other service goes to the database jsonItem = service.getProductById("1"); //get product by id 1 assertEquals(jsonItem.getId(), FIRST_PRODUCT_ID); // I compare the id on which I made the request to the database, so that I came and was wrapped in a class wrapper }如何正確執(zhí)行,以免在測(cè)試中配置連接?
1 回答

Qyouu
TA貢獻(xiàn)1786條經(jīng)驗(yàn) 獲得超11個(gè)贊
Javalin將是模擬真實(shí)服務(wù)的絕佳工具,因?yàn)樗试S在測(cè)試中進(jìn)行狀態(tài)斷言。
也可以使用Wiremock 。但這會(huì)導(dǎo)致難以維護(hù)行為測(cè)試(驗(yàn)證)。
添加回答
舉報(bào)
0/150
提交
取消