[英]Mockito - mocking classes with native methods
我有简单的测试用例:
@Test
public void test() throws Exception{
TableElement table = mock(TableElement.class);
table.insertRow(0);
}
其中TableElement是GWT类,方法insertRow
定义为:
public final native TableRowElement insertRow(int index);
当我开始测试时,我得到:
java.lang.UnsatisfiedLinkError: com.google.gwt.dom.client.TableElement.insertRow(I)Lcom/google/gwt/dom/client/TableRowElement;
at com.google.gwt.dom.client.TableElement.insertRow(Native Method)
我相信哪个与insertRow方法有关。 有没有办法或解决方法来模拟Mockito的这些方法?
Mockito本身似乎无法根据此Google Group线程模拟本机方法。 但是,您有两个选择:
将TableElement
类包装在一个接口中并模拟该接口以正确测试您的SUT调用包装的insertRow(...)
方法。 缺点是您需要添加额外的接口(当GWT项目应该在他们自己的API中完成此操作时)以及使用它的开销。 接口的代码和具体实现如下所示:
// the mockable interface public interface ITableElementWrapper { public void insertRow(int index); } // the concrete implementation that you'll be using public class TableElementWrapper implements ITableElementWrapper { TableElement wrapped; public TableElementWrapper(TableElement te) { this.wrapped = te; } public void insertRow(int index) { wrapped.insertRow(index); } } // the factory that your SUT should be injected with and be // using to wrap the table element with public interface IGwtWrapperFactory { public ITableElementWrapper wrap(TableElement te); } public class GwtWrapperFactory implements IGwtWrapperFactory { public ITableElementWrapper wrap(TableElement te) { return new TableElementWrapper(te); } }
PowerMockito
的Mockito API扩展来模拟本机方法。 缺点是您有另一个依赖项加载到您的测试项目(我知道这可能是一些组织的问题,其中第三方库必须首先被审计才能被使用)。 我个人会选择2,因为GWT项目不可能在接口中包装自己的类(并且它更可能有更多需要模拟的本机方法)并且自己做它只包装本机方法打电话只是浪费你的时间。
万一其他人偶然发现:在此期间( 2013年5月 ) GwtMockito出现了,这解决了这个问题,没有PowerMock的开销。
试试这个
@RunWith(GwtMockitoTestRunner.class)
public class MyTest {
@Test
public void test() throws Exception{
TableElement table = mock(TableElement.class);
table.insertRow(0);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.