簡體   English   中英

使用 EasyMock 模擬方法

[英]mock methods with EasyMock

我嘗試使用以下方法創建單元測試,但我找不到模擬每個方法內部調用的解決方案,請您幫我使用 EasyMock 為這些方法創建 Junit 測試:

private static final WebServiceCache<JWebService> SERVICE = new WebServiceCache<>();
public int getCount() {
    int res = -1;
    try {
        String count = SERVICE.invokeSecurelly(new WS<String>() {
            @Override
            public String execute() throws Exception {
                return getWS().getList();
            }
        });
        res = Integer.parseInt(count);
    } catch (Exception e) {
        LOGGER.error("Count Exception" + e);
    }
    return res;
}

public int getKeyNumber() {
    int res = -1;
    try {
        String keyId = SERVICE.invokeSecurelly(new WS<String>() {
            @Override
            public String execute() throws Exception {
                return getWS().getID();
            }
        });
        
        res = Integer.parseInt(keyId);
    } catch (Exception e) {
        LOGGER.error("getKeyNumBer returns an error" + e);
    }
    return res;
}

提前致謝

為了做一些干凈的事情,您需要進行一些重構以使其可測試。 這就是我最終會得到的......猜測你的例子中缺少的核心的一部分。

public class MyClass {
    private static final Logger LOGGER = Logger.getLogger(MyClass.class.getName());
    private final WebServiceCache<JWebService> service;

    public MyClass(WebServiceCache<JWebService> service) {
        this.service = service;
    }
    
    private int getValue(Supplier<String> invoked) {
        try {
            String count = service.invokeSecurelly(invoked::get);
            return Integer.parseInt(count);
        } catch (Exception e) {
            LOGGER.error("Count Exception", e);
        }
        return -1;
    }

    public int getCount() {
        return getValue(() -> getWS().getList());
    }

    public int getKeyNumber() {
        return getValue(() -> getWS().getID());
    }

    private Stuff getWS() { // guessing where getWS() is
        return null;
    }
}

從那里開始,如果我們假設您要模擬的是getWS() ,它看起來像這樣。

import org.easymock.EasyMockSupport;
import org.junit.Before;
import org.junit.Test;

import static org.assertj.core.api.Assertions.assertThat;
import static org.easymock.EasyMock.expect;

public class MyClassTest extends EasyMockSupport {

    private WebServiceCache<JWebService> cache = new WebServiceCache<>();
    private MyClass tested = partialMockBuilder(MyClass.class)
            .addMockedMethod("getWS")
            .withConstructor(cache)
            .mock();
    private Stuff stuff = mock(Stuff.class);

    @Before
    public void before() {
        expect(tested.getWS()).andStubReturn(stuff);
    }

    @Test
    public void getCount() {
        expect(stuff.getList()).andStubReturn("8");
        replayAll();

        assertThat(tested.getCount()).isEqualTo(8);
    }

    @Test
    public void getKeyNumber() {
        expect(stuff.getID()).andStubReturn("8");
        replayAll();

        assertThat(tested.getKeyNumber()).isEqualTo(8);
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM