Skip to content
This repository has been archived by the owner on Mar 11, 2022. It is now read-only.

Commit

Permalink
Junit 5 (#422)
Browse files Browse the repository at this point in the history
* Upgrade tests to JUnit 5 (JUnit Jupiter)

* Refactor tests to use JUnit 5 (org.junit.jupiter.api.*) APIs

* Convert existing Resources/Rules etc to JUnit 5 Extensions

* Add base classes in com.cloudant.tests.base package for common test
  patterns eg remote-data-base-per-class, remote-dat abase-per-method

* Convert parameterised tests to TestTemplates which use helper
  classes to provide parameters

* Convert JUnit 4 Categories to JUnit 5 Tags

* Upgrade gradle to 4.6

* Add custom JavaExec task to build.gradle to invoke JUnit Platform
  Console Launcher, due to lack of flexibility in current gradle
  support for JUnit Platform.

* Use Java 1.6 for production code and 1.8 for test code
  • Loading branch information
tomblench committed Mar 28, 2018
1 parent 118a86c commit 3529583
Show file tree
Hide file tree
Showing 69 changed files with 3,637 additions and 2,635 deletions.
4 changes: 2 additions & 2 deletions Jenkinsfile
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!groovy

/*
* Copyright © 2016, 2017 IBM Corp. All rights reserved.
* Copyright © 2016, 2018 IBM Corp. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of the License at
Expand Down Expand Up @@ -31,7 +31,7 @@ def runTests(testEnv, isServiceTests) {
try {
sh './gradlew -Dtest.couch.username=$DB_USER -Dtest.couch.password=$DB_PASSWORD -Dtest.couch.host=$DB_HOST -Dtest.couch.port=$DB_PORT -Dtest.couch.http=$DB_HTTP $GRADLE_TARGET'
} finally {
junit '**/build/test-results/*.xml'
junit '**/build/test-results/**/*.xml'
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright © 2016 IBM Corp. All rights reserved.
* Copyright © 2016, 2018 IBM Corp. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of the License at
Expand Down
71 changes: 45 additions & 26 deletions cloudant-client/build.gradle
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright © 2016, 2017 IBM Corp. All rights reserved.
* Copyright © 2016, 2018 IBM Corp. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of the License at
Expand All @@ -25,7 +25,10 @@ dependencies {
compile project(':cloudant-http')
linkableJavadoc project(':cloudant-http')
//test dependencies
testCompile group: 'junit', name: 'junit', version: '4.12'
testRuntime group: 'org.junit.jupiter', name: 'junit-jupiter-engine', version: '5.1.0'
testRuntime group: 'org.junit.platform', name: 'junit-platform-console', version: '1.1.0'
testCompile group: 'org.junit.jupiter', name: 'junit-jupiter-api', version: '5.1.0'
testCompile group: 'org.hamcrest', name: 'hamcrest-library', version: '1.3'
testCompile group: 'com.squareup.okhttp3', name: 'mockwebserver', version: '3.8.1'
testCompile group: 'org.jmockit', name: 'jmockit', version: '1.34'
testCompile group: 'org.littleshoot', name: 'littleproxy', version: '1.1.0'
Expand Down Expand Up @@ -54,6 +57,12 @@ gradle.projectsEvaluated {
}
}

// we need Java 1.8 features for JUnit 5 features, but our production code is 1.6
compileTestJava {
sourceCompatibility = 1.8
targetCompatibility = 1.8
}

tasks.withType(Test) {
def jMockit
doFirst {
Expand All @@ -65,35 +74,45 @@ tasks.withType(Test) {
}
}

test {
// Run tests for any DB
useJUnit {
excludeCategories 'com.cloudant.test.main.RequiresCloudant',
'com.cloudant.test.main.RequiresCouch'
// hacky - see https://discuss.gradle.org/t/passing-arguments-to-a-task/8427/9
def CustomJunitPlatformTask(include, exclude) {
return tasks.create(name: "exec_${include}_${exclude}", type: JavaExec, dependsOn: testClasses) {
classpath = sourceSets.main.runtimeClasspath + sourceSets.test.runtimeClasspath
main = 'org.junit.platform.console.ConsoleLauncher'
// arguments to pass to the application
args ('--reports-dir',testResultsDir,
'--scan-class-path')
// build up includes
if (include.size > 0) {
args += '--include-tag'
args += include.join("|")
}
// build up excludes
if (exclude.size > 0) {
args += '--exclude-tag'
args += exclude.join("|")
}
// inherit system properties
systemProperties System.properties
// some tests expect user.dir to be same as working dir, which
// defaults to project dir
systemProperty "user.dir", project.projectDir
}
}

task noDBTest(type: Test, dependsOn: testClasses) {
// Run unit tests that do not need a running database
useJUnit {
excludeCategories 'com.cloudant.test.main.RequiresDB'
}
}
// Run tests which are compatible with all versions of Couch or
// Cloudant.
// This is the default test target.
task test (dependsOn:CustomJunitPlatformTask([],['RequiresCouch','RequiresCloudant']), overwrite: true){}

task cloudantTest(type: Test, dependsOn: testClasses) {
// Run tests that can use any Cloudant
useJUnit {
excludeCategories 'com.cloudant.test.main.RequiresCloudantService'
}
}
// Run 'unit' tests which do not require a database.
task noDBTest(dependsOn: CustomJunitPlatformTask([],['RequiresDB'])) {}

task cloudantServiceTest(type: Test, dependsOn: testClasses) {
// Run all Cloudant service tests
useJUnit {
excludeCategories 'com.cloudant.test.main.RequiresCloudantLocal',
'com.cloudant.test.main.RequiresCouch'
}
}
// Run tests that can use any Cloudant.
task cloudantTest(dependsOn: CustomJunitPlatformTask([],['RequiresCloudantService'])) {}

// Run all Cloudant service tests.
task cloudantServiceTest(dependsOn: CustomJunitPlatformTask([],['RequiresCloudantLocal', 'RequiresCouch'])) {}

//task for generating a client properties file
class ClientProperties extends DefaultTask {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright © 2017 IBM Corp. All rights reserved.
* Copyright © 2017, 2018 IBM Corp. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of the License at
Expand All @@ -14,8 +14,8 @@

package com.cloudant.api.query;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;

import com.cloudant.client.api.query.Field;
import com.cloudant.client.api.query.JsonIndex;
Expand All @@ -40,7 +40,7 @@ public static class Json extends FieldAssertHelper<Sort.Order, JsonIndex.Field>

@Override
protected void assertField(JsonIndex.Field field, Sort.Order order) {
assertEquals("The order should be the same", order, field.getOrder());
assertEquals(order, field.getOrder(), "The order should be the same");
}
}

Expand All @@ -53,20 +53,20 @@ public static class Text extends FieldAssertHelper<Type, TextIndex.Field> {

@Override
protected void assertField(TextIndex.Field field, Type type) {
assertEquals("The type should be the same", type, field.getType());
assertEquals(type, field.getType(), "The type should be the same");
}
}

public void assertFields(List<F> actualFields) {
assertEquals("There should be the correct number of fields", expectedFields.size(),
actualFields.size());
assertEquals(expectedFields.size(),
actualFields.size(), "There should be the correct number of fields");
for (F field : actualFields) {
assertNotNull("The field should have a name", field.getName());
T expected = expectedFields.remove(field.getName());
assertNotNull("Unexpected field " + field.getName() + " found.", expected);
assertNotNull(expected, "Unexpected field " + field.getName() + " found.");
assertField(field, expected);
}
assertEquals("All fields should be asserted.", 0, expectedFields.size());
assertEquals(0, expectedFields.size(), "All fields should be asserted.");
}

protected abstract void assertField(F field, T type);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,24 +15,24 @@
package com.cloudant.api.query;

import static com.cloudant.client.api.query.Expression.gt;
import static org.junit.Assert.assertEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;

import com.cloudant.client.api.query.JsonIndex;
import com.cloudant.client.api.query.Selector;
import com.cloudant.client.api.query.TextIndex;
import com.cloudant.client.internal.query.Helpers;
import com.cloudant.tests.util.MockedServerTest;
import com.cloudant.tests.base.TestWithMockedServer;
import com.google.gson.Gson;
import com.google.gson.JsonObject;

import org.junit.Test;
import org.junit.jupiter.api.Test;

import okhttp3.mockwebserver.MockResponse;
import okhttp3.mockwebserver.RecordedRequest;

import java.util.concurrent.TimeUnit;

public class IndexCreationTests extends MockedServerTest {
public class IndexCreationTests extends TestWithMockedServer {

private static MockResponse CREATED = new MockResponse().setBody("{\"result\": \"created\"}");

Expand Down Expand Up @@ -122,7 +122,7 @@ public void createNamedTextIndex() throws Exception {
@Test
public void createTextIndexInDesignDoc() throws Exception {
createIndexTest(TextIndex.builder()
.designDocument("testddoc")
.designDocument("testddoc")
.definition(),
"{type: \"text\", ddoc: \"testddoc\", index: {}}");
}
Expand Down Expand Up @@ -216,6 +216,6 @@ private void createIndexTest(String definition, String expected) throws Exceptio
db.createIndex(definition);
RecordedRequest request = server.takeRequest(1, TimeUnit.SECONDS);
JsonObject actual = new Gson().fromJson(request.getBody().readUtf8(), JsonObject.class);
assertEquals("The request body should match the expected", exp, actual);
assertEquals(exp, actual, "The request body should match the expected");
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright © 2017 IBM Corp. All rights reserved.
* Copyright © 2017, 2018 IBM Corp. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of the License at
Expand All @@ -14,21 +14,21 @@

package com.cloudant.api.query;

import static org.junit.Assert.assertEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;

import com.cloudant.tests.base.TestWithMockedServer;
import com.cloudant.tests.util.MockWebServerResources;
import com.cloudant.tests.util.MockedServerTest;

import org.junit.Before;
import org.junit.Test;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import okhttp3.mockwebserver.RecordedRequest;

import java.util.concurrent.TimeUnit;

public class IndexDeletionTests extends MockedServerTest {
public class IndexDeletionTests extends TestWithMockedServer {

@Before
@BeforeEach
public void enqueueOK() {
server.enqueue(MockWebServerResources.JSON_OK);
}
Expand All @@ -53,7 +53,7 @@ public void deleteTextIndex() throws Exception {

private void assertDelete(String name, String ddoc, String type) throws Exception {
RecordedRequest request = server.takeRequest(1, TimeUnit.SECONDS);
assertEquals("The request body should match the expected", "/" + dbResource.getDatabaseName() +
"/_index/_design/" + ddoc + "/" + type + "/" + name, request.getPath());
assertEquals("/" + dbResource.getDatabaseName() + "/_index/_design/" + ddoc + "/" + type
+ "/" + name, request.getPath(), "The request body should match the expected");
}
}
Loading

0 comments on commit 3529583

Please sign in to comment.