Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/402.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/225.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 非模拟Android集成测试_Java_Android_Unit Testing - Fatal编程技术网

Java 非模拟Android集成测试

Java 非模拟Android集成测试,java,android,unit-testing,Java,Android,Unit Testing,在iOS中,我可以做这样的集成测试 // Setup expecttation to prevent from test ending before all async tasks finish. let expectation = expectationWithDescription("Sign in"); // call API method for signing in PersonAPI.signIn("asdf@asdf.co", password: "Free Milk Lane"

在iOS中,我可以做这样的集成测试

// Setup expecttation to prevent from test ending before all async tasks finish.
let expectation = expectationWithDescription("Sign in");

// call API method for signing in
PersonAPI.signIn("asdf@asdf.co", password: "Free Milk Lane", done:{(response: APIResponse)->Void in

    // check response for errors
    XCTAssertTrue(response.isSuccessful() == true, response.getMessage());

    // mark async operation is completed
    expectation.fulfill();
});

// wait until all async operations completed
waitForExpectationsWithTimeout(5.0, handler:nil);
但在安卓系统中,这并不明显。现在我正试图使用机器人电气和改装,但它只是不想合作。我已经尝试了很多方法,我认为问题与的线程如何汇集有关。例如,以下代码将通过,但无论如何都要等待5秒,即使API调用可能只需要1秒:

// Setup signal to prevent from test ending before all async tasks finish.
final CountDownLatch signal = new CountDownLatch(1);

// call API method for signing in
PersonAPI.signIn("asdf@asdf.co", "Free Milk Lane", new IAPICallback() {public void done(APIResponse response) {

    // check response for errors
    Assert.assertTrue(response.getMessage(), response.isSuccessful());

    // mark async operation is completed
    signal.countDown();
}});

// wait until all async operations completed
signal.await(5, TimeUnit.SECONDS);
在这一点上,我愿意尝试任何事情(除了嘲笑)。改型,重新充电,随便什么


再次感谢

以下是我是如何做到这一点的。我把改装改成了同步通话。然后,我将响应存储在一个静态变量中,并在主线程上完成断言

public class PersonAPITest {
        private static APIResponse _response = null;

        @Test
        public void testSignIn() {
            // Setup signal to prevent from test ending before all async tasks finish.
            final CountDownLatch signal = new CountDownLatch(1);

            // call API method for signing in
            PersonAPI.signIn("asdf@asdf.co", "Free Milk Lane", new IAPICallback() {public void done(APIResponse response) {

               // check response for errors
               PersonAPITest._response = response;

               // mark async operation is completed
               signal.countDown();
            }});

          // wait until all async operations completed
          signal.await(5, TimeUnit.SECONDS);

          Assert.assertTrue(PersonAPITest._response.getMessage(), PersonAPITest._response.isSuccessful());
        }
    }
下面是改装代码的示例。基本上,我只是使用本机Java多线程来创建异步方法:

public static void getGoogle(final IAPICallback callback) {
        new Thread(new Runnable() {

            @Override
            public void run() {

                RestAdapter.Builder builder = new RestAdapter.Builder()
                        .setEndpoint("http://google.com")
                        .setClient(new OkClient(new OkHttpClient()));
                RestAdapter adapter = builder.build();
                ITaskAPI service = (ITaskAPI) adapter.create(ITaskAPI.class);

                Response result = service.getGoogle();

                APIResponse response;

                if(result.getStatus() == 200) {
                    response = new APIResponse(true, "it worked!");
                }else{
                    response = new APIResponse(false, "boo! bad dog!");
                }

                callback.done(response);
            }
        }).start();
    }