Unit Testing Location Service

前端 未结 2 1122
星月不相逢
星月不相逢 2021-02-06 07:04

I\'ve got a location tracking service that I\'m trying to unit test. I\'m trying to use the locationManager.addTestProvider and setTestProviderLocation methods to achieve this.

2条回答
  •  旧时难觅i
    2021-02-06 07:28

    I was able to get @ingsaurabh's example working in a unit test after making the following modifications:

    import java.io.IOException;
    import java.io.OutputStreamWriter;
    import java.io.Writer;
    import java.net.Socket;
    import java.net.UnknownHostException;
    
    public final class TestLocationProvider {
        static void sendLocation(double latitude, double longitude) {
            try {
                Socket socket = new Socket("10.0.2.2", 5554); // usually 5554
                socket.setKeepAlive(true);
                String str = "geo fix " + longitude + " " + latitude ;
                Writer w = new OutputStreamWriter(socket.getOutputStream());
                w.write(str + "\r\n");
                w.flush();
            }
            catch (UnknownHostException e) {
                throw new RuntimeException(e);
            }
            catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
    }
    

    The important detail is the IP address. The emulator does not recognize 'localhost' as a valid hostname.

提交回复
热议问题