错误 java.lang.RuntimeException:存根!在 Android 中使用 Fitnesse 测试

发布于 2024-12-28 14:53:29 字数 3745 浏览 3 评论 0原文

我正在尝试使用 Fitnesse 框架创建一个测试装置,并且我想测试一个从服务器检索数据的函数(RESTFUL 服务)。我的测试用例非常简单:

public class FriendListActivityFixture extends ColumnFixture {
    public int URL;
    public String test() {
    JSONArray array = JsonHelper.getJsonArrayFromUrl("http://107.22.209.62/android/get_users.php");
    return array.toString();
    }
}

public static JSONArray getJsonArrayFromUrl(String url) {
        InputStream input = null;
        String result = "";
        JSONArray jsonArray = null;
        try {
            HttpClient httpclient = CustomHttpClient.getHttpClient();
            HttpPost httppost = new HttpPost(url);
            HttpResponse response = httpclient.execute(httppost);
            HttpEntity entity = response.getEntity();
            input = entity.getContent();
        }
        catch (Exception e) {
            Log.e(TAG + ".getJsonArrayFromUrl(String url)", "Error in http connection " + e.toString()); 
        }

        try {
            BufferedReader reader = new BufferedReader(new InputStreamReader(input, "iso-8859-1"), 8);
            StringBuilder content = new StringBuilder();
            String line = null;
            while ((line = reader.readLine()) != null) {
                content.append(line + "\n");
            }
            input.close();
            result = content.toString();
        }
        catch (Exception e) {
            Log.e(TAG + ".getJsonArrayFromUrl(String url)", "Error parsing result " + e.toString());
        }

        try {
            jsonArray = new JSONArray(result);
        }
        catch (JSONException e) {
            Log.e(TAG + "getJsonArrayFromUrl(String url)", "Error converting data " + e.toString());
        }
        return jsonArray;
    }

这是 Fitnesse 测试页面:!pathfitnesse.jar

!path C:\Program Files (x86)\Android\android-sdk\platforms\android-10\android.jar
!path C:\Users\chan\git\Spotr\Spotr\bin\classes

!|com.csun.spotr.fitnesse.FriendListActivityFixture|
|URL|test?|
|0|"wwa"|

因为它只是一个演示,所以我的测试目前看起来可能有点傻。不幸的是,我不断收到这些错误:

java.lang.RuntimeException: Stub!
    at android.util.Log.e(Log.java:15)
    at com.csun.spotr.util.JsonHelper.getJsonArrayFromUrl(JsonHelper.java:75)
    at com.csun.spotr.fitnesse.FriendListActivityFixture.test(FriendListActivityFixture.java:30)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at fit.TypeAdapter.invoke(TypeAdapter.java:109)
    at fit.TypeAdapter.get(TypeAdapter.java:97)
    at fit.Fixture$CellComparator.compareCellToResult(Fixture.java:371)
    at fit.Fixture$CellComparator.access$100(Fixture.java:357)
    at fit.Fixture.compareCellToResult(Fixture.java:299)
    at fit.Fixture.check(Fixture.java:295)
    at fit.ColumnFixture.check(ColumnFixture.java:51)
    at fit.Binding$QueryBinding.doCell(Binding.java:215)
    at fit.ColumnFixture.doCell(ColumnFixture.java:37)
    at fit.Fixture.doCells(Fixture.java:171)
    at fit.Fixture.doRow(Fixture.java:165)
    at fit.ColumnFixture.doRow(ColumnFixture.java:25)
    at fit.Fixture.doRows(Fixture.java:159)
    at fit.ColumnFixture.doRows(ColumnFixture.java:18)
    at fit.Fixture.doTable(Fixture.java:153)
    at fit.Fixture.interpretTables(Fixture.java:99)
    at fit.Fixture.doTables(Fixture.java:79)
    at fit.FitServer.process(FitServer.java:81)
    at fit.FitServer.run(FitServer.java:56)
    at fit.FitServer.main(FitServer.java:41)

我不知道它告诉我什么?我编写了其他测试方法,例如 add()、substract(),一切正常。我想知道这个错误是否涉及在主线程上运行长任务?有什么想法吗?

I'm trying to create a test fixture using Fitnesse framework, and I want to test a function which retrieves data from a server (RESTFUL service). My test case is very simple:

public class FriendListActivityFixture extends ColumnFixture {
    public int URL;
    public String test() {
    JSONArray array = JsonHelper.getJsonArrayFromUrl("http://107.22.209.62/android/get_users.php");
    return array.toString();
    }
}

public static JSONArray getJsonArrayFromUrl(String url) {
        InputStream input = null;
        String result = "";
        JSONArray jsonArray = null;
        try {
            HttpClient httpclient = CustomHttpClient.getHttpClient();
            HttpPost httppost = new HttpPost(url);
            HttpResponse response = httpclient.execute(httppost);
            HttpEntity entity = response.getEntity();
            input = entity.getContent();
        }
        catch (Exception e) {
            Log.e(TAG + ".getJsonArrayFromUrl(String url)", "Error in http connection " + e.toString()); 
        }

        try {
            BufferedReader reader = new BufferedReader(new InputStreamReader(input, "iso-8859-1"), 8);
            StringBuilder content = new StringBuilder();
            String line = null;
            while ((line = reader.readLine()) != null) {
                content.append(line + "\n");
            }
            input.close();
            result = content.toString();
        }
        catch (Exception e) {
            Log.e(TAG + ".getJsonArrayFromUrl(String url)", "Error parsing result " + e.toString());
        }

        try {
            jsonArray = new JSONArray(result);
        }
        catch (JSONException e) {
            Log.e(TAG + "getJsonArrayFromUrl(String url)", "Error converting data " + e.toString());
        }
        return jsonArray;
    }

And here is the Fitnesse test page:!path fitnesse.jar

!path C:\Program Files (x86)\Android\android-sdk\platforms\android-10\android.jar
!path C:\Users\chan\git\Spotr\Spotr\bin\classes

!|com.csun.spotr.fitnesse.FriendListActivityFixture|
|URL|test?|
|0|"wwa"|

Since it's just a demo, my test might look a bit silly at the moment. Unfortunately, I keep getting these errors:

java.lang.RuntimeException: Stub!
    at android.util.Log.e(Log.java:15)
    at com.csun.spotr.util.JsonHelper.getJsonArrayFromUrl(JsonHelper.java:75)
    at com.csun.spotr.fitnesse.FriendListActivityFixture.test(FriendListActivityFixture.java:30)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at fit.TypeAdapter.invoke(TypeAdapter.java:109)
    at fit.TypeAdapter.get(TypeAdapter.java:97)
    at fit.Fixture$CellComparator.compareCellToResult(Fixture.java:371)
    at fit.Fixture$CellComparator.access$100(Fixture.java:357)
    at fit.Fixture.compareCellToResult(Fixture.java:299)
    at fit.Fixture.check(Fixture.java:295)
    at fit.ColumnFixture.check(ColumnFixture.java:51)
    at fit.Binding$QueryBinding.doCell(Binding.java:215)
    at fit.ColumnFixture.doCell(ColumnFixture.java:37)
    at fit.Fixture.doCells(Fixture.java:171)
    at fit.Fixture.doRow(Fixture.java:165)
    at fit.ColumnFixture.doRow(ColumnFixture.java:25)
    at fit.Fixture.doRows(Fixture.java:159)
    at fit.ColumnFixture.doRows(ColumnFixture.java:18)
    at fit.Fixture.doTable(Fixture.java:153)
    at fit.Fixture.interpretTables(Fixture.java:99)
    at fit.Fixture.doTables(Fixture.java:79)
    at fit.FitServer.process(FitServer.java:81)
    at fit.FitServer.run(FitServer.java:56)
    at fit.FitServer.main(FitServer.java:41)

And I have no idea what is it telling me? I wrote other testing method like add(), substract(), everything worked fine. I wonder does this error involve running a long task on the main thread? Any idea?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

伊面 2025-01-04 14:53:29

android.jar 仅包含类的存根实现。它为您提供了构建应用程序的方法,一旦您拥有 APK,您必须在 Android 设备或模拟器上运行它。

如果我没记错的话,你正在尝试在主机的 JVM 上运行。

android.jar contains only stub implementation of the classes. It provides the means for you app to build, once you have your APK you must run it on an android device or emulator.

If I'm not wrong you are trying to run on host's JVM.

海夕 2025-01-04 14:53:29

您可以使用 Roboelectric 轻松解决此问题并在 IDE 中进行测试。

正如 dtmilano 所说,您无法按原样在笔记本电脑上运行 Android 代码,但 Roboelectric 基本上用实际方法实现替换了 Android.jar 中的存根。

如果您采用此解决方案,需要注意两件事:

  • 如果使用 IntelliJ,请确保您的 JAR 导入位于依赖项列表中的 Android JAR 之上

  • 假设您实际上不想在某处更改服务器的状态,它默认会阻止 HTTP 请求。但是,您可以很容易地禁用此功能: Roboelectric.getFakeHttpLayer().interceptHttpRequests(false);

You can fix this problem and test within your IDE really easily by using Roboelectric.

As dtmilano said, you can't run Android code on your laptop as is, but Roboelectric basically substitutes the actual method implementations for the stubs in Android.jar.

Two things to watch out for if you go with this solution:

  • Make sure that your JAR import is above the Android JAR in your dependencies list if using IntelliJ

  • It defaults to blocking HTTP requests under the assumption that you don't actually want to change the state of a server somewhere. You can, however, disable this quite easily: Roboelectric.getFakeHttpLayer().interceptHttpRequests(false);

自找没趣 2025-01-04 14:53:29

如前所述,Android jar 除了编译之外没有任何用处。即使是最无辜的事情也被彻底消灭了。但您仍然可以通过良好的模拟框架在主机 VM 单元测试中使用它们。我的选择是 jmockit

不确定它是否可以与 Fitnesse 一起使用

As mentioned previously, Android jars are not good for anything beyond compiling. Even most innocent things are stubbed out completely. But you can still use them in host VM unit tests with good mocking frameworks. My choice is jmockit:

Not sure whether it will work with Fitnesse though

音盲 2025-01-04 14:53:29

由于“静态”声明,我遇到了同样的问题。就我而言,.build() 导致静态变量出现问题。简单的解决方案是不将变量声明为静态。它可以是最终的。尝试从 getJsonArrayFromUrl 方法中删除“static”,看看它是否有效。

I had this same issue because of the "static" declaration. In my case, .build() caused the issue in a static variable. The simple solution was to not declare the variable static. It can be final. Try removing "static" from the getJsonArrayFromUrl method and see if it works.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文