-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #66 from webcompere/test-ng
Add support for TestNG
- Loading branch information
Showing
10 changed files
with
530 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
# System Stubs TestNG | ||
|
||
Provides some automatic instantiation of System Stubs objects during the test lifecycle. | ||
|
||
```xml | ||
<dependency> | ||
<groupId>uk.org.webcompere</groupId> | ||
<artifactId>system-stubs-testng</artifactId> | ||
<version>2.1.1</version> | ||
</dependency> | ||
``` | ||
|
||
## Options | ||
|
||
### Stub Without the Plugin | ||
System Stubs Core can be used with TestNG as it is framework agnostic. | ||
|
||
We can call the `setup` method on any of the stubs in a before method, and `teardown` in the after method. | ||
|
||
```java | ||
private EnvironmentVariables environmentVariables = new EnvironmentVariables(); | ||
|
||
@BeforeTest | ||
public void beforeTest() throws Exception { | ||
environmentVariables.set("setinbefore", "yes"); | ||
|
||
environmentVariables.setup(); | ||
} | ||
|
||
@AfterTest | ||
public void afterTest() throws Exception { | ||
environmentVariables.teardown(); | ||
} | ||
``` | ||
|
||
With this code, we'd expect tests to be able to modify the runtime environment by manipulating the | ||
`environmentVariables` object, and we'd expect the tests to have an environment variable `setinbefore` set | ||
to `yes`. | ||
|
||
Similarly, we can use `setup` and `teardown` inside a test case, or use the `SystemStubs` methods such as | ||
`withEnvironmentVariables`. See the [main documentation](../README.md) for more on the execute around pattern. | ||
|
||
### Using of the Plugin | ||
|
||
The plugin: | ||
|
||
- Automatically instantiates system stubs objects before they're first used by a TestNG annotated method | ||
- Activates the objects during tests | ||
- Turns the objects off after tests | ||
|
||
Usage: | ||
|
||
```java | ||
@Listeners(SystemStubsListener.class) | ||
public class CaptureSystemOutTest { | ||
|
||
@SystemStub | ||
private SystemOut out; | ||
|
||
@BeforeTest | ||
public void beforeTest() { | ||
out.clear(); | ||
} | ||
|
||
@Test | ||
public void canReadThingsSentToSystemOut() { | ||
// simulate the system under test writing to std out | ||
System.out.println("Can I assert this?"); | ||
|
||
assertThat(out.getText()).isEqualTo("Can I assert this?\n"); | ||
} | ||
} | ||
``` | ||
|
||
> Note: in this instance we've used the `SystemOut` stub. We've had to remember to call its `clear` method as it | ||
> will be shared between tests. | ||
We can use each of the stubs such as: | ||
|
||
- `EnvironmentVariables` - for overriding the environment variables | ||
- `SystemProperties` - for temporarily overwriting system properties and then restoring them afterwards | ||
- `SystemOut` - for tapping the `System.out` | ||
- ... and the others | ||
|
||
All we need to do is: | ||
|
||
- Add the `@Listeners(SystemStubsListener.class)` annotation to our TestNG test class (using an array with {} if we have other listeners) | ||
- Add a field for each System Stub we want to use | ||
- Annotate that field with the `@SystemStubs` annotation | ||
|
||
### Benefits of the Plugin | ||
|
||
With the plugin, there's less boilerplate to write. Any exception handling is also covered by the plugin - or at | ||
least, we don't have to explicitly add `throws` to any of our methods that set up or teardown a stub. | ||
|
||
However, the plugin is simple and opinionated. For fine-grained control of the stubs, the direct method | ||
may sometimes be preferable. | ||
|
||
## Feedback | ||
|
||
This TestNG module is incubating. Please raise issues with examples if it proves to have issues in practice. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<parent> | ||
<groupId>uk.org.webcompere</groupId> | ||
<artifactId>system-stubs-parent</artifactId> | ||
<version>2.1.2-SNAPSHOT</version> | ||
<relativePath>../pom.xml</relativePath> | ||
</parent> | ||
|
||
|
||
<artifactId>system-stubs-testng</artifactId> | ||
<version>2.1.2-SNAPSHOT</version> | ||
<packaging>jar</packaging> | ||
|
||
<properties> | ||
<maven.compiler.source>11</maven.compiler.source> | ||
<maven.compiler.target>11</maven.compiler.target> | ||
</properties> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>uk.org.webcompere</groupId> | ||
<artifactId>system-stubs-core</artifactId> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>com.github.spotbugs</groupId> | ||
<artifactId>spotbugs-annotations</artifactId> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>org.testng</groupId> | ||
<artifactId>testng</artifactId> | ||
<scope>provided</scope> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>org.assertj</groupId> | ||
<artifactId>assertj-core</artifactId> | ||
<scope>test</scope> | ||
</dependency> | ||
</dependencies> | ||
|
||
<build> | ||
<plugins> | ||
<plugin> | ||
<groupId>org.apache.maven.plugins</groupId> | ||
<artifactId>maven-surefire-plugin</artifactId> | ||
</plugin> | ||
<plugin> | ||
<groupId>org.jacoco</groupId> | ||
<artifactId>jacoco-maven-plugin</artifactId> | ||
</plugin> | ||
<plugin> | ||
<groupId>org.apache.maven.plugins</groupId> | ||
<artifactId>maven-checkstyle-plugin</artifactId> | ||
</plugin> | ||
<plugin> | ||
<groupId>com.github.spotbugs</groupId> | ||
<artifactId>spotbugs-maven-plugin</artifactId> | ||
</plugin> | ||
|
||
<!-- For release --> | ||
<plugin> | ||
<groupId>org.apache.maven.plugins</groupId> | ||
<artifactId>maven-gpg-plugin</artifactId> | ||
</plugin> | ||
<plugin> | ||
<groupId>org.apache.maven.plugins</groupId> | ||
<artifactId>maven-source-plugin</artifactId> | ||
</plugin> | ||
<plugin> | ||
<groupId>org.apache.maven.plugins</groupId> | ||
<artifactId>maven-javadoc-plugin</artifactId> | ||
</plugin> | ||
</plugins> | ||
</build> | ||
|
||
</project> |
16 changes: 16 additions & 0 deletions
16
system-stubs-testng/src/main/java/uk/org/webcompere/systemstubs/testng/SystemStub.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package uk.org.webcompere.systemstubs.testng; | ||
|
||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
|
||
/** | ||
* Marks a field in a test class as a system stub - this causes the {@link SystemStubsListener} to activate | ||
* it during tests. It also causes the field to become instantiated if left uninitialized | ||
* @since 1.0.0 | ||
*/ | ||
@Target({ ElementType.FIELD }) | ||
@Retention(RetentionPolicy.RUNTIME) | ||
public @interface SystemStub { | ||
} |
Oops, something went wrong.