【问题标题】:mock methods with EasyMock使用 EasyMock 模拟方法
【发布时间】:2020-09-15 14:57:25
【问题描述】:

我尝试从以下方法创建单元测试,但我找不到在每个方法中模拟调用的解决方案,请您帮助我使用 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;
}

提前致谢

【问题讨论】:

    标签: java unit-testing mockito junit4 easymock


    【解决方案1】:

    要做一些干净的事情,您需要进行一些重构以使其可测试。这就是我最终的结果......在你的例子中猜测部分缺失的核心。

    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);
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多