spark-test

A library that allows the test of Spark Web Framework based applications using HTTP

License

License

GroupId

GroupId

com.despegar
ArtifactId

ArtifactId

spark-test
Last Version

Last Version

1.1.8
Release Date

Release Date

Type

Type

jar
Description

Description

spark-test
A library that allows the test of Spark Web Framework based applications using HTTP
Project URL

Project URL

https://github.com/despegar/spark-test
Source Code Management

Source Code Management

https://github.com/despegar/spark-test.git

Download spark-test

How to add to project

<!-- https://jarcasting.com/artifacts/com.despegar/spark-test/ -->
<dependency>
    <groupId>com.despegar</groupId>
    <artifactId>spark-test</artifactId>
    <version>1.1.8</version>
</dependency>
// https://jarcasting.com/artifacts/com.despegar/spark-test/
implementation 'com.despegar:spark-test:1.1.8'
// https://jarcasting.com/artifacts/com.despegar/spark-test/
implementation ("com.despegar:spark-test:1.1.8")
'com.despegar:spark-test:jar:1.1.8'
<dependency org="com.despegar" name="spark-test" rev="1.1.8">
  <artifact name="spark-test" type="jar" />
</dependency>
@Grapes(
@Grab(group='com.despegar', module='spark-test', version='1.1.8')
)
libraryDependencies += "com.despegar" % "spark-test" % "1.1.8"
[com.despegar/spark-test "1.1.8"]

Dependencies

compile (3)

Group / Artifact Type Version
com.sparkjava : spark-core jar [2.2, )
com.despegar : http-java-native-client jar 1.0.4
junit : junit jar [4.7, 4.12]

test (1)

Group / Artifact Type Version
org.slf4j : slf4j-simple jar 1.6.1

Project Modules

There are no modules declared in this project.

Spark Web Framework Test Library

This library allows testing Spark Web Framework based applications through HTTP. This way, the complete flow of the application can be tested, from the parsing of a HTTP request through business logic, executing asserts against HTTP responses headers, status, body, etc.

Getting Started

Dependency

Maven

Add to your pom.xml dependencies

<dependency>
    <groupId>com.despegar</groupId>
    <artifactId>spark-test</artifactId>
    <version>1.1.8</version>
</dependency>

Gradle

Add to your build.gradle dependencies:

dependencies {
    testCompile "com.despegar:spark-test:1.1.8"
}

Usage

/**
 * The class that defines a Spark Web Framework route
 *
 */
public class TestController {

	public TestController() {
		get("/test", (request, response) ->  this.testMethod(request, response));
	}

	public String testMethod(Request request, Response response) {
		return "This works!";
	}

}
/**
 * The test class
 *
 */
public class TestControllerTest {

	public static class TestContollerTestSparkApplication implements SparkApplication {
		@Override
		public void init() {
			new TestController();
		}
	}

	@ClassRule
	public static SparkServer<TestContollerTestSparkApplication> testServer = new SparkServer<>(TestControllerTest.TestContollerTestSparkApplication.class, 4567);

	@Test
	public void test() throws Exception {
		/* The second parameter indicates whether redirects must be followed or not */
		GetMethod get = testServer.get("/test", false);
		get.addHeader("Test-Header", "test");
		HttpResponse httpResponse = testServer.execute(get);
		assertEquals(200, httpResponse.code());
		assertEquals("This works!", new String(httpResponse.body()));
		assertNotNull(testServer.getApplication());
	}

}

Now http-java-native-client is used. The HTTP response body is a byte[]. You can parse it as necessary.

com.despegar

Despegar

Creamos experiencias de viaje que enriquecen tu vida

Versions

Version
1.1.8
1.1.6
1.1.5
1.1.4
1.1.3
1.1.2
1.1.1
1.1
1.0