Skip to content

Commit 7f84a22

Browse files
committed
#178 Added ParameterizedTest to unit-test project
1 parent 7b96a6f commit 7f84a22

File tree

6 files changed

+132
-17
lines changed

6 files changed

+132
-17
lines changed

build.gradle

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,11 @@ targetCompatibility = "1.8"
1515
repositories {
1616
mavenLocal() // Used for local development only
1717
jcenter()
18+
19+
// Needed until ml-unit-test is available via jcenter()
20+
maven {
21+
url {"https://dl.bintray.com/rjrudin/maven/"}
22+
}
1823
}
1924

2025
dependencies {
@@ -24,7 +29,7 @@ dependencies {
2429
compile mlcpUtilDependency
2530
compile group: 'commons-io', name: 'commons-io', version: '2.5'
2631

27-
compileOnly "com.marklogic:marklogic-unit-test-client:0.4.0"
32+
compileOnly "com.marklogic:ml-unit-test-client:0.9.1"
2833

2934
testCompile localGroovy()
3035
testCompile gradleTestKit()

examples/local-testing-project/gradle.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Set this to the version you used when running
22
# "gradle -Pversion=(something) publishToMavenLocal" on your local ml-gradle repo
3-
mlGradleVersion=277
3+
mlGradleVersion=3.5.0
44

55
mlHost=localhost
66
mlAppName=example
Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,30 @@
1-
This project shows an example of using the marklogic-unit-test framework to run server-side tests within MarkLogic.
1+
This project shows an example of using the ml-unit-test framework to run server-side tests within MarkLogic.
22

3-
Using marklogic-unit-test requires two additions to the build.gradle file:
3+
Using ml-unit-test requires two additions to the build.gradle file, as described below.
44

5-
1. ml-gradle includes an "mlUnitTest" task, which depends on the marklogic-unit-test-client JAR file. ml-gradle does not
6-
include this by default (not every ml-gradle user will use marklogic-unit-test), so it must be added to the buildscript:
5+
First, ml-gradle includes an "mlUnitTest" task, which depends on the ml-unit-test-client JAR file. ml-gradle does not
6+
include this by default (not every ml-gradle user will use ml-unit-test), so it must be added to the buildscript:
77

88
buildscript {
99
repositories {
1010
jcenter()
1111
}
1212
dependencies {
13-
classpath "com.marklogic:marklogic-unit-test-client:1.0"
13+
classpath "com.marklogic:ml-unit-test-client:0.9.1"
1414
}
1515
}
1616

17-
1. The marklogic-unit-test framework is depended on and installed as an "mlRestApi" dependency:
17+
Next, the ml-unit-test framework is depended on and installed as an "mlRestApi" dependency
1818

19+
// Needed for ml-unit-test-client dependency until it's available via jcenter()
20+
repositories {
21+
maven {
22+
url {"https://dl.bintray.com/rjrudin/maven/"}
23+
}
24+
}
25+
1926
dependencies {
20-
mlRestApi "com.marklogic:marklogic-unit-test:1.0"
27+
mlRestApi "com.marklogic:ml-unit-test:0.9.1"
2128
}
2229

2330
With those additions in place, the "mlUnitTest" task can be run. This task will use the value of mlTestRestPort to
@@ -27,10 +34,27 @@ First, deploy the application:
2734

2835
gradle mlDeploy
2936

30-
This will deploy the application along with the marklogic-unit-test modules.
37+
This will deploy the application along with the ml-unit-test modules.
3138

3239
Then, run the tests:
3340

3441
gradle mlUnitTest
3542

3643
Two tests are run, and one should fail, so you can see what a failed test looks like.
44+
45+
This project includes the Gradle Java plugin, which allows you to run tests under src/test/java. This project includes
46+
an example of a JUnit Parameterized test that invokes each ml-unit-test module separately - you can try it like this:
47+
48+
gradle test
49+
50+
Again, two tests will run, and one will intentionally fail. The Parameterized test can be run in an IDE as well, allowing
51+
you to take advantage of your IDE's support for JUnit tests.
52+
53+
You can also access the ml-unit-test REST endpoints directly:
54+
55+
- List the tests - http://localhost:8135/v1/resources/ml-unit-test
56+
- Run a test suite - http://localhost:8135/v1/resources/ml-unit-test?rs:func=run&rs:suite=My%20Tests
57+
58+
And you can run the original UI test runner by going to:
59+
60+
- http://localhost:8135/test/default.xqy
Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
buildscript {
22
repositories {
3-
mavenLocal()
43
jcenter()
4+
5+
// Needed for ml-unit-test-client dependency until it's available via jcenter()
6+
maven {
7+
url {"https://dl.bintray.com/rjrudin/maven/"}
8+
}
59
}
610
dependencies {
7-
classpath "com.marklogic:marklogic-unit-test-client:0.4.0"
11+
classpath "com.marklogic:ml-unit-test-client:0.9.1"
812
classpath "com.marklogic:ml-gradle:${mlGradleVersion}"
913
}
1014
}
@@ -13,10 +17,18 @@ apply plugin: "java"
1317
apply plugin: "com.marklogic.ml-gradle"
1418

1519
repositories {
16-
mavenLocal()
1720
jcenter()
21+
22+
// Needed for ml-unit-test dependency until it's available via jcenter()
23+
maven {
24+
url {"https://dl.bintray.com/rjrudin/maven/"}
25+
}
1826
}
1927

2028
dependencies {
21-
mlRestApi "com.marklogic:marklogic-unit-test:0.4.0"
29+
mlRestApi "com.marklogic:ml-unit-test:0.9.1"
30+
31+
// For running ml-unit-test tests via JUnit
32+
testCompile "com.marklogic:ml-unit-test-client:0.9.1"
33+
testCompile "junit:junit:4+"
2234
}

examples/unit-test-project/gradle.properties

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
# Set this to the version you used when running
2-
# "gradle -Pversion=(something) publishToMavenLocal" on your local ml-gradle repo
3-
mlGradleVersion=277
1+
mlGradleVersion=3.5.0
42

53
mlHost=localhost
64
mlAppName=unit-test-example
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
package example;
2+
3+
import com.marklogic.client.DatabaseClient;
4+
import com.marklogic.client.DatabaseClientFactory;
5+
import com.marklogic.test.unit.TestManager;
6+
import com.marklogic.test.unit.TestModule;
7+
import com.marklogic.test.unit.TestResult;
8+
import com.marklogic.test.unit.TestSuiteResult;
9+
import org.junit.AfterClass;
10+
import org.junit.Assert;
11+
import org.junit.Test;
12+
import org.junit.runner.RunWith;
13+
import org.junit.runners.Parameterized;
14+
15+
import java.io.FileReader;
16+
import java.io.IOException;
17+
import java.util.List;
18+
import java.util.Properties;
19+
20+
/**
21+
* Example of running each test module as a separate JUnit test.
22+
*/
23+
@RunWith(Parameterized.class)
24+
public class ParameterizedTest extends Assert {
25+
26+
private TestModule testModule;
27+
28+
private static TestManager testManager;
29+
private static DatabaseClient databaseClient;
30+
31+
public ParameterizedTest(TestModule testModule) {
32+
this.testModule = testModule;
33+
}
34+
35+
@AfterClass
36+
public static void releaseDatabaseClient() {
37+
if (databaseClient != null) {
38+
databaseClient.release();
39+
}
40+
}
41+
42+
/**
43+
* This sets up the parameters for our test by getting a list of the test modules.
44+
* <p>
45+
* Also creates a DatabaseClient based on the values in gradle.properties. Typically, properties will be retrieved via
46+
* a more robust mechanism, like Spring's test framework support.
47+
*
48+
* @return
49+
* @throws Exception
50+
*/
51+
@Parameterized.Parameters(name = "{index}: {0}")
52+
public static List<TestModule> getTestModules() throws IOException {
53+
Properties props = new Properties();
54+
props.load(new FileReader("gradle.properties"));
55+
final String host = props.getProperty("mlHost");
56+
final int port = Integer.parseInt(props.getProperty("mlTestRestPort"));
57+
final String username = props.getProperty("mlUsername");
58+
final String password = props.getProperty("mlPassword");
59+
60+
databaseClient = DatabaseClientFactory.newClient(host, port,
61+
new DatabaseClientFactory.DigestAuthContext(username, password));
62+
testManager = new TestManager(databaseClient);
63+
return testManager.list();
64+
}
65+
66+
@Test
67+
public void test() {
68+
TestSuiteResult result = testManager.run(this.testModule);
69+
for (TestResult testResult : result.getTestResults()) {
70+
String failureXml = testResult.getFailureXml();
71+
if (failureXml != null) {
72+
fail(String.format("Test %s in suite %s failed, cause: %s", testResult.getName(), testModule.getSuite(), failureXml));
73+
}
74+
}
75+
}
76+
}

0 commit comments

Comments
 (0)