vraptor-test

Sonatype helps open source projects to set up Maven repositories on https://oss.sonatype.org/

GroupId

GroupId

br.com.caelum.vraptor
ArtifactId

ArtifactId

vraptor-test
Last Version

Last Version

4.1.0
Release Date

Release Date

Type

Type

jar
Description

Description

vraptor-test
Sonatype helps open source projects to set up Maven repositories on https://oss.sonatype.org/
Project URL

Project URL

http://maven.apache.org
Source Code Management

Source Code Management

http://github.com/caelum/vraptor-test

Download vraptor-test

How to add to project

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

Dependencies

compile (7)

Group / Artifact Type Version
br.com.caelum : vraptor jar 4.1.4
junit : junit jar 4.8.2
org.springframework : spring-test jar 4.0.5.RELEASE
log4j : log4j jar 1.2.17
org.slf4j : slf4j-log4j12 jar 1.7.5
org.apache.tomcat : tomcat-jasper jar 7.0.77
org.apache.ant : ant jar 1.9.2

provided (8)

Group / Artifact Type Version
org.jboss.weld.servlet : weld-servlet jar 2.1.0.Final
org.jboss.weld.se : weld-se-core jar 2.1.0.Final
javax.servlet : javax.servlet-api jar 3.1.0
org.glassfish.web : javax.el jar 2.2.4
javax.validation : validation-api jar 1.1.0.Final
org.hibernate : hibernate-validator jar 5.0.1.Final
org.hibernate : hibernate-validator-cdi jar 5.0.1.Final
javax.servlet : jstl jar 1.2

test (3)

Group / Artifact Type Version
org.mockito : mockito-core jar 1.8.5
org.hamcrest : hamcrest-core jar 1.3
org.hamcrest : hamcrest-library jar 1.3

Project Modules

There are no modules declared in this project.

vraptor-test

Build Status

A VRaptor plugin that allows you to quickly create integration or system tests.

This plugin currently works only with VRaptor 4.x versions. For support to VRaptor 3, check vraptor-test 1.x versions (being developed in vraptor3 branch: https://github.com/caelum/vraptor-test/tree/vraptor3).

Warning: this plugin works only with Java <= 7

installing

Add to your pom:

<dependency>
	<groupId>br.com.caelum.vraptor</groupId>
	<artifactId>vraptor-test</artifactId>
	<version>4.1.0-SNAPSHOT</version>
	<scope>test</scope>
</dependency>

Or simply copy all jars to your classpath.

creating a system wide test

To create a test with vraptor-test you only need to create a junit test class that extends our base class, VRaptorIntegration:

public class IntegrationTest extends VRaptorIntegration {

	@Test
	public void shouldIncludeAtrributeInResult() throws Exception {
		VRaptorTestResult result = navigate().get("/test/test").execute();
	}
}

This test case will execute a request to /test/test url in your application and return a VRaptorTestResult object. Note that this test will really execute the code of your controller.

To add headers, sometimes for authentication, for example:

@Test
public void shouldPassObjectHeaders() {
	String name = "Authorization";
	String value = "Bearer 123";
	VRaptorTestResult result = navigate()
					.post("/test/test11")
					.addHeader(name, value)
					.execute();
}

This is the result:

POST /test/test11 HTTP/1.1
Host: http://myhost/test/test11
Accept: application/json, text/javascript, */*; q=0.01
Authorization: Bearer 123
Referer: http://myhost/test/test11
Accept-Encoding: gzip, deflate, sdch

The VRaptorTestResult object allow you to verify things that happened during the request:

@Test
public void shouldIncludeAtrributeInResult() throws Exception {
	VRaptorTestResult result = navigate().get("/test/test").execute();
	result.wasStatus(200).isValid();
	assertEquals("vraptor", result.getObject("name"));
}

In the second line of the test, we verify that the http status code of the request was 200 and that there was no validation errors (included in vraptor's Validator class). If any of this two conditions had failed, the test would fail imediatly (you don't need to write any assert).

In the third line, we verify if a attribute was included in the result (in a result.include in the controller being tested, for example). The result.getObject method returns the object included or null if it wasn't included.

You can also add parameters to be sent in the request:

@Test
public void shouldIncludeAtrributeInResult() throws Exception {
	VRaptorTestResult result = navigate()
					.post("/tasks")
					.addParameter("task.description", "Task description")
					.addParameter("task.name", "Task name")
					.execute();
	Task task = result.getObject("task"));
	assertEquals("Task description", task.getDescription());
	assertEquals("Task name", task.getName());
}

vraptor-test also compile and execute jsp files. You can get the final output written to the response from the result object:

@Test
public void shouldCompileAndExecuteAJsp() {
	VRaptorTestResult result = navigate().post("/task/1").execute();
	String html = result.getResponseBody();
	assertEquals("<h1>Task name</h1>", html);
}

For other examples, check the vraptor-test tests :-)

https://github.com/caelum/vraptor-test/blob/master/src/test/java/br/com/caelum/vraptor/test/VRaptorNavigationSimpleScenariosTest.java

br.com.caelum.vraptor

Caelum

Versions

Version
4.1.0
4.0.0
2.0.0
1.0.0