我如何在 android 应用程序中模拟 REST 后端
How do i mock REST backend in an android app
我的 android 应用程序通过 REST API 与后端服务通信。我想模拟出这个API来快速开发前端。
我正在使用 android volley 作为客户端网络库。
您可以为此使用 dependency injection 设计模式。
基本上,您指定一个接口来定义一组方法,这些方法与您在 REST 后端中的查询相对应,例如:
interface DataSupplier {
// Lookup user by ID
User getUser(int id);
// Get all blog posts posted by a specific user.
List<BlogPost> getUsersBlogPosts(int userId);
}
现在在class需要查询后端的地方,指定一个注入器。这可以通过多种方式完成(例如,构造函数注入,setter 注入 - 请参阅 wiki 文章了解更多详细信息)。注入器允许您将依赖项的实现注入到依赖它的 class 中。让我们假设您使用构造函数注入。使用后端的 class 将如下所示:
public class DependentClass {
private final DataSupplier mSupplier;
public DependentClass(DataSupplier dataSupplier) {
mSupplier = dataSupplier;
}
// Now you simply call mSupplier whenever you need to query the mock
// (or - later in development - the real) REST service, e.g.:
public void printUserName() {
System.out.println("User name: " + mSupplier.getUser(42).getName());
}
}
然后创建 DataSupplier
:
的模拟实现
public class MockRestService implements DataSupplier {
@Override
public User getUser(int id) {
// Return a dummy user that matches the given ID
// with 'Alice' as the username.
return new User(id, "Alice");
}
@Override
public List<BlogPost> getUsersBlogPosts(int userId) {
List<BlogPost> result = new ArrayList<BlogPost>();
result.add(new BlogPost("Some Title", "Some body text"));
result.add(new BlogPost("Another Title", "Another body text"));
result.add(new BlogPost("A Third Title", "A third body text"));
return result;
}
}
并用它来实例化你的依赖 class:
DepedentClass restClient = new DepedentClass(new MockRestService());
现在您可以使用 restClient
,就像它已连接到您的实际后端一样。它只是 return 虚拟对象,您可以使用它们来开发前端。
当您完成前端并准备好实施后端时,您可以通过创建 DataSupplier
的另一个实施来实现,该实施建立与您的 REST 后端的连接并查询它以获取真实对象。假设您将此实现命名为 RestService
。现在您可以简单地用 RestService
构造函数替换创建 MockRestService
的构造函数,如下所示:
DepedentClass restClient = new DepedentClass(new RestService());
就这样:通过交换单个构造函数调用,您可以将前端代码从使用虚拟对象更改为使用真正的 REST 交付对象。
您甚至可以拥有调试标志并根据应用程序的状态(调试或发布)创建 restClient
:
boolean debug = true;
DependentClass restClient = null;
if (debug) {
restClient = new DepedentClass(new MockRestService());
} else {
restClient = new DepedentClass(new RestService());
}
我最近创建了 RESTMock。它是一个用于在 android 测试中模拟 REST API 的库。它可以在开发过程中使用。您需要按照 github 上的自述文件对其进行设置,并创建一个基本的 Android 仪器测试,该测试将启动您的应用程序并且什么也不做。这样应用程序就可以在后台使用模拟服务器启动。
示例测试:
public class SmokeTest {
@Rule public ActivityTestRule<MainActivity> rule = new ActivityTestRule<MainActivity>(
SplashActivity.class,
true,
false);
@Test
public void smokeTest() throws InterruptedException {
rule.launchActivity(null);
Thread.sleep(10000000);
}
}
我的 android 应用程序通过 REST API 与后端服务通信。我想模拟出这个API来快速开发前端。 我正在使用 android volley 作为客户端网络库。
您可以为此使用 dependency injection 设计模式。
基本上,您指定一个接口来定义一组方法,这些方法与您在 REST 后端中的查询相对应,例如:
interface DataSupplier {
// Lookup user by ID
User getUser(int id);
// Get all blog posts posted by a specific user.
List<BlogPost> getUsersBlogPosts(int userId);
}
现在在class需要查询后端的地方,指定一个注入器。这可以通过多种方式完成(例如,构造函数注入,setter 注入 - 请参阅 wiki 文章了解更多详细信息)。注入器允许您将依赖项的实现注入到依赖它的 class 中。让我们假设您使用构造函数注入。使用后端的 class 将如下所示:
public class DependentClass {
private final DataSupplier mSupplier;
public DependentClass(DataSupplier dataSupplier) {
mSupplier = dataSupplier;
}
// Now you simply call mSupplier whenever you need to query the mock
// (or - later in development - the real) REST service, e.g.:
public void printUserName() {
System.out.println("User name: " + mSupplier.getUser(42).getName());
}
}
然后创建 DataSupplier
:
public class MockRestService implements DataSupplier {
@Override
public User getUser(int id) {
// Return a dummy user that matches the given ID
// with 'Alice' as the username.
return new User(id, "Alice");
}
@Override
public List<BlogPost> getUsersBlogPosts(int userId) {
List<BlogPost> result = new ArrayList<BlogPost>();
result.add(new BlogPost("Some Title", "Some body text"));
result.add(new BlogPost("Another Title", "Another body text"));
result.add(new BlogPost("A Third Title", "A third body text"));
return result;
}
}
并用它来实例化你的依赖 class:
DepedentClass restClient = new DepedentClass(new MockRestService());
现在您可以使用 restClient
,就像它已连接到您的实际后端一样。它只是 return 虚拟对象,您可以使用它们来开发前端。
当您完成前端并准备好实施后端时,您可以通过创建 DataSupplier
的另一个实施来实现,该实施建立与您的 REST 后端的连接并查询它以获取真实对象。假设您将此实现命名为 RestService
。现在您可以简单地用 RestService
构造函数替换创建 MockRestService
的构造函数,如下所示:
DepedentClass restClient = new DepedentClass(new RestService());
就这样:通过交换单个构造函数调用,您可以将前端代码从使用虚拟对象更改为使用真正的 REST 交付对象。
您甚至可以拥有调试标志并根据应用程序的状态(调试或发布)创建 restClient
:
boolean debug = true;
DependentClass restClient = null;
if (debug) {
restClient = new DepedentClass(new MockRestService());
} else {
restClient = new DepedentClass(new RestService());
}
我最近创建了 RESTMock。它是一个用于在 android 测试中模拟 REST API 的库。它可以在开发过程中使用。您需要按照 github 上的自述文件对其进行设置,并创建一个基本的 Android 仪器测试,该测试将启动您的应用程序并且什么也不做。这样应用程序就可以在后台使用模拟服务器启动。
示例测试:
public class SmokeTest {
@Rule public ActivityTestRule<MainActivity> rule = new ActivityTestRule<MainActivity>(
SplashActivity.class,
true,
false);
@Test
public void smokeTest() throws InterruptedException {
rule.launchActivity(null);
Thread.sleep(10000000);
}
}