[例]Android Testing的完整参考有实例.
Complete reference for Android Testing with examples.
A unit test generally exercises the functionality of the smallest possible unit of code (which could be a method, class, or component) in a repeatable way.
Tools that are used to do this testing:
A UI Test or Instrumentation Test mocks typical user interactions with your app. Clicking on buttons, typing in text are some of the things UI Tests can complete.
There are other tools that are available for this kind of testing such as Robotium, Appium, Calabash, Robolectric.
public class Calculator {
public int add(int op1, int op2) {
return op1 + op2;
}
public int diff(int op1, int op2) {
return op1 - op2;
}
public double div(int op1, int op2) {
// if (op2 == 0) return 0;
return op1 / op2;
}
}…@Ignore
@Test(expected = java.lang.ArithmeticException.class)
public void testDivWithZeroDivisor() {
calculator = new Calculator();
double total = calculator.div(9, 0);
assertEquals("Calculator is not handling division by zero correctly", 0.0, total, 0.0);
}JUnit provides overloaded assertion methods for all primitive types and Objects and arrays (of primitives or Objects). The parameter order is expected value followed by actual value. Optionally the first parameter can be a String message that is output on failure. There is a slightly different assertion, assertThat that has parameters of the optional failure message, the actual value, and a Matcher object. Note that expected and actual are reversed compared to the other assert methods.
……public class CalculatorWithTestName {
@Rule
public TestName name = new TestName();
@Test
public void testAdd() {
Calculator calculator = new Calculator();
int total = calculator.add(4, 5);
assertEquals(name.getMethodName() + " adding incorrectly", 9, total);
}
@Test
public void testDiff() {
Calculator calculator = new Calculator();
int total = calculator.diff(12, 7);
assertEquals(name.getMethodName() + " subtracting incorrectly", 5, total);
}
}RESTMock is a library working on top of Square's okhttp/MockWebServer. It allows you to specify Hamcrest matchers to match HTTP requests and specify what response to return. It is as easy as:
RESTMockServer.whenGET(pathContains("users/defunkt"))
.thenReturnFile(200, "users/defunkt.json");It's good to start server before the tested application starts, there are few methods:
To make it simple you can just use the predefined RESTMockTestRunner in your UI tests. It extends AndroidJUnitRunner:
defaultConfig {
...
testInstrumentationRunner 'io.appflate.restmock.android.RESTMockTestRunner'
}If you have your custom test runner and you can't extend RESTMockTestRunner, you can always just call the RESTMockServerStarter. Actually RESTMockTestRunner is doing exactly the same thing:
public class MyAppTestRunner extends AndroidJUnitRunner {
...
@Override
public void onCreate(Bundle arguments) {
super.onCreate(arguments);
RESTMockServerStarter.startSync(new AndroidAssetsFileParser(getContext()),new AndroidLogger());
...
}
...
}
By default, the RESTMockTestRunner uses AndroidAssetsFileParser as a mocks file parser, which reads the files from the assets folder. To make them visible for the RESTMock you have to put them in the correct folder in your project, for example:
.../src/androidTest/assets/users/defunkt.json
This can be accessed like this:
RESTMockServer.whenGET(pathContains("users/defunkt"))
.thenReturnFile(200, "users/defunkt.json");If the response You wish to return is simple, you can just specify a string:
RESTMockServer.whenGET(pathContains("users/defunkt"))
.thenReturnString(200, "{}");If you wish to have a greater control over the response, you can pass the MockResponse
RESTMockServer.whenGET(pathContains("users/defunkt")).thenReturn(new MockResponse().setBody("").setResponseCode(401).addHeader("Header","Value"));You can either use some of the predefined matchers from RequestMatchers util class, or create your own. remember to extend from RequestMatcher
The most important step, in order for your app to communicate with the testServer, you have to specify it as an endpoint for all your API calls. For that, you can use the RESTMockServer.getUrl(). If you use Retrofit, it is as easy as:
RestAdapter adapter = new RestAdapter.Builder()
.baseUrl(RESTMockServer.getUrl())
...
.build();It is possible to verify which requests were called and how many times thanks to RequestsVerifier. All you have to do is call one of these:
//cheks if the GET request was invoked exactly 2 times
RequestsVerifier.verifyGET(pathEndsWith("users")).exactly(2);
//cheks if the GET request was invoked at least 3 times
RequestsVerifier.verifyGET(pathEndsWith("users")).atLeast(3);
//cheks if the GET request was invoked exactly 1 time
RequestsVerifier.verifyGET(pathEndsWith("users")).invoked();
//cheks if the GET request was never invoked
RequestsVerifier.verifyGET(pathEndsWith("users")).never();RESTMock supports logging events. You just have to provide the RESTMock with the implementation of RESTMockLogger. For Android there is an AndroidLogger implemented already. All you have to do is use the RESTMockTestRunner or call
RESTMockServerStarter.startSync(new AndroidAssetsFileParser(getContext()),new AndroidLogger());or
RESTMockServer.enableLogging(RESTMockLogger)
RESTMockServer.disableLogging()………………MainActivityRoboelectricTest.java
@RunWith(RobolectricGradleTestRunner.class)
@Config(constants = BuildConfig.class)
public class MainActivityRoboelectricTest {
private Mai暂无开放 Issues,或尚未同步最近议题。