Pio*_*zyk 10 java gwt native-methods mockito
我有简单的测试用例:
@Test
public void test() throws Exception{
TableElement table = mock(TableElement.class);
table.insertRow(0);
}
Run Code Online (Sandbox Code Playgroud)
其中TableElement是GWT类,其方法insertRow
定义为:
public final native TableRowElement insertRow(int index);
Run Code Online (Sandbox Code Playgroud)
当我开始测试时,我得到:
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)
Run Code Online (Sandbox Code Playgroud)
我相信哪个与insertRow方法有关.有没有办法或解决方法来模拟Mockito的这些方法?
Spo*_*ike 11
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);
}
}
Run Code Online (Sandbox Code Playgroud)PowerMockito
嘲笑本地方法.缺点是您有另一个依赖项加载到您的测试项目(我知道这可能是一些组织的问题,其中第三方库必须首先被审计才能被使用).我个人会选择2,因为GWT项目不太可能在接口中包装自己的类(并且它更可能有更多需要模拟的本机方法)并且自己做它只包装本机方法打电话只是浪费你的时间.
归档时间: |
|
查看次数: |
5811 次 |
最近记录: |