mirror of
https://github.com/alibaba/testable-mock.git
synced 2025-01-10 20:30:11 +08:00
114 lines
5.1 KiB
Markdown
114 lines
5.1 KiB
Markdown
Use TestableMock
|
||
---
|
||
|
||
`TestableMock` is now not only a lightweight and easy-to-use unit testing mock tool, but also a comprehensive set of auxiliary tools aimed at **simplifying Java unit testing**, including the following functions:
|
||
|
||
- [Quickly mock arbitrary call](en-us/doc/use-mock.md): quickly replace any method invocation in the class under test with a mock method, solve the cumbersome use of traditional mock tools problem
|
||
- [Access private members of the class under test](en-us/doc/private-accessor.md): enable unit tests directly invoke or access private members of the class under test, solve the problems of private member initialization and private method testing
|
||
- [Quickly construct complicated parameter object](en-us/doc/omni-constructor.md):generate arbitrarily nested object instances, simplify their internal member assignment methods, solve the problem of long initialization codes for method parameters
|
||
- [Assist test void method](en-us/doc/test-void-method.md): use the mock validator to check the internal logic of method, solve the problem that unit testing is difficult to implement to the method with no return value
|
||
- [Assist test SQL generated by mybatis](en-us/doc/verify-sql.md): provide built-in mock implementation for common sql libraries, solve the problem that the logic in data-access-object (DAO) layer cannot be tested directly
|
||
|
||
## Use in Maven project
|
||
|
||
In the project `pom.xml` file, add `testable-all` dependency and `maven-surefire-plugin` configuration, the specific method is as follows.
|
||
|
||
It is recommended to add a `property` field that identifies the TestableMock version, in order to simplify version management:
|
||
|
||
```xml
|
||
<properties>
|
||
<testable.version>0.6.5</testable.version>
|
||
</properties>
|
||
```
|
||
|
||
Add dependence of `TestableMock` inside `dependencies` field:
|
||
|
||
```xml
|
||
<dependencies>
|
||
<dependency>
|
||
<groupId>com.alibaba.testable</groupId>
|
||
<artifactId>testable-all</artifactId>
|
||
<version>${testable.version}</version>
|
||
<scope>test</scope>
|
||
</dependency>
|
||
</dependencies>
|
||
```
|
||
|
||
Finally, add the `maven-surefire-plugin` plugin to the `plugins` list in the `build` area (if this plugin is already included, just add the `<argLine>` part of the configuration):
|
||
|
||
```xml
|
||
<build>
|
||
<plugins>
|
||
<plugin>
|
||
<groupId>org.apache.maven.plugins</groupId>
|
||
<artifactId>maven-surefire-plugin</artifactId>
|
||
<configuration>
|
||
<argLine>-javaagent:${settings.localRepository}/com/alibaba/testable/testable-agent/${testable.version}/testable-agent-${testable.version}.jar</argLine>
|
||
</configuration>
|
||
</plugin>
|
||
</plugins>
|
||
</build>
|
||
```
|
||
|
||
If the project also uses the `on-the-fly` mode of `Jacoco` (default mode) to calculate the unit test coverage, you need to add a `@{argLine}` parameter in the `<argLine>` configuration, after adding it The configuration is as follows:
|
||
|
||
```xml
|
||
<argLine>@{argLine} -javaagent:${settings.localRepository}/com/alibaba/testable/testable-agent/${testable.version}/testable-agent-${testable.version}.jar</argLine>
|
||
```
|
||
|
||
See the [pom.xml](https://github.com/alibaba/testable-mock/blob/master/demo/java-demo/pom.xml) file of project `java-demo` and the [pom.xml](https://github.com/alibaba/testable-mock/blob/master/demo/kotlin-demo/pom.xml) file of project `kotlin-demo`.
|
||
|
||
## Use in Gradle project
|
||
|
||
Add dependence of `TestableMock` in `build.gradle` file:
|
||
|
||
```groovy
|
||
dependencies {
|
||
testImplementation('com.alibaba.testable:testable-all:0.6.5')
|
||
testAnnotationProcessor('com.alibaba.testable:testable-processor:0.6.5')
|
||
}
|
||
```
|
||
|
||
Then add `javaagent` to "test" configuration:
|
||
|
||
```groovy
|
||
test {
|
||
jvmArgs "-javaagent:${classpath.find { it.name.contains("testable-agent") }.absolutePath}"
|
||
}
|
||
```
|
||
|
||
See the [build.gradle](https://github.com/alibaba/testable-mock/blob/master/demo/java-demo/build.gradle) file of project `java-demo` and the [build.gradle.kts](https://github.com/alibaba/testable-mock/blob/master/demo/kotlin-demo/build.gradle.kts) file of project `kotlin-demo`.
|
||
|
||
> For Android project, please use the same method to add `TestableMock` dependency as above, and add `javaagent` configuration as follows:
|
||
>
|
||
> ```groovy
|
||
> android {
|
||
> testOptions {
|
||
> unitTests {
|
||
> all {
|
||
> jvmArgs "-javaagent:${classpath.find { it.name.contains("testable-agent") }.absolutePath}"
|
||
> }
|
||
> }
|
||
> }
|
||
> }
|
||
> ```
|
||
>
|
||
> See project `demo/android-demo` for a complete example.
|
||
|
||
> If the project is using `Spock` test framework, you need to specify the bytecode version generated by `Groovy` to be 1.6 or above, the method is as follows (please modify the properties value according to the actual JVM version used).
|
||
>
|
||
> For Maven project, add `<maven.compiler.source>` and `<maven.compiler.target>` properties inside the `pom.xml` file, e.g.
|
||
> ```xml
|
||
> <properties>
|
||
> <maven.compiler.source>1.6</maven.compiler.source>
|
||
> <maven.compiler.target>1.6</maven.compiler.target>
|
||
> </properties>
|
||
> ```
|
||
>
|
||
> For Gradle project, add a `sourceCompatibility` property inside the `build.gradle` file, e.g.
|
||
> ```groovy
|
||
> sourceCompatibility = '6'
|
||
> ```
|
||
>
|
||
> See project `demo/spock-demo` for a complete example.
|