How to Set Up Java, Eclipse, Maven and REST Assured
In this beginner-friendly tutorial, we will prepare a Java API testing environment from scratch, create a Maven project in Eclipse, add REST Assured dependencies, write our first API test, and run it from both Eclipse and the command line.
- Install a Java Development Kit (JDK) and verify the installation.
- Configure
JAVA_HOMEandPATHon Windows. - Install Eclipse and create a Maven project.
- Understand
pom.xmland add REST Assured and test framework dependencies. - Write a simple GET API test and run it successfully.
- Troubleshoot common setup, dependency, and test execution errors.
1. Prerequisites for Learning REST Assured
REST Assured is a Java library. Before writing REST Assured tests, you need Java, an IDE or text editor, a build tool to download dependencies, and a test framework to execute and report test results.
| Tool | Purpose | Recommendation |
|---|---|---|
| Java JDK | Compiles and runs Java code. | Use a supported LTS JDK such as Java 17 or 21, subject to your project requirements. |
| Eclipse IDE | Editor for writing, organizing and debugging Java code. | Use a current Eclipse package suitable for Java development. |
| Apache Maven | Manages libraries and runs build/test tasks. | Eclipse often includes Maven integration; standalone Maven is optional for command-line use. |
| REST Assured | Sends HTTP requests and validates API responses. | Add it as a Maven dependency. |
| JUnit 5 or TestNG | Discovers, runs and organizes tests. | This tutorial uses JUnit 5 to keep the first example focused. |
Tool websites and package names can change over time. Download software from official sources and check the Java version required by the selected Eclipse release and your organization's standards.
2. How to Install Java JDK
The Java Development Kit (JDK) includes the tools needed to compile and run Java programs. A JRE-only installation is not enough for compiling your own test code.
Step 1: Download a JDK
- Open the official Eclipse Temurin website or the Oracle Java downloads page.
- Choose a supported JDK version for your operating system and processor architecture.
- Download the installer or archive and follow the vendor's installation instructions.
- Note the installation directory. On Windows, it may resemble
C:\Program Files\Eclipse Adoptium\jdk-21..., but the exact path depends on the package and version.
Step 2: Verify Java
Open a new Command Prompt or terminal and run:
java -version
javac -version
The first command prints the Java runtime version; the second prints the compiler version. Both should run successfully, and the versions should be compatible.
17 or 21. Your exact output will depend on the installed JDK.3. How to Configure JAVA_HOME and PATH on Windows
JAVA_HOME is an environment variable that points to the JDK installation directory. PATH helps Windows find commands such as java and javac from a terminal.
Step-by-step configuration
- Search Windows for Edit the system environment variables and open it.
- Select Environment Variables.
- Under User variables (or System variables if your administrator manages it), choose New.
- Set the variable name to
JAVA_HOMEand the value to your JDK folder, such asC:\Program Files\Eclipse Adoptium\jdk-21. Use your actual installation path. - Edit the
Pathvariable and add a new entry:%JAVA_HOME%\bin. - Save the dialogs and open a new terminal window so it picks up the updated environment.
Verify the configuration with:
echo %JAVA_HOME%
where java
java -version
javac -version
In Windows PowerShell, you can also use $env:JAVA_HOME to display the variable.
JAVA_HOME to the JDK folder itself, not to its bin subfolder and not to a command or text containing extra symbols. For example, if the compiler is at C:\Java\jdk-21\bin\javac.exe, the JAVA_HOME value is normally C:\Java\jdk-21.On macOS or Linux, environment-variable configuration depends on the shell and installation method. Follow the JDK vendor's instructions for your platform.
4. How to Install Eclipse IDE
Eclipse is an integrated development environment (IDE) that helps you write Java code, manage projects, run tests, and debug errors.
Steps
- Visit the official Eclipse downloads page.
- Download the Eclipse Installer for your operating system.
- Choose the package intended for Java developers (the wording may vary by installer version).
- Complete installation and launch Eclipse.
- Choose a workspace folder. The workspace stores Eclipse project metadata and settings; your project source files can be located inside it or in another chosen directory.
Check the Java configuration in Eclipse
- Open Window → Preferences on Windows/Linux, or Eclipse → Settings/Preferences on macOS (menu wording can vary).
- Find Java → Installed JREs.
- Confirm that a compatible JDK is listed. If necessary, add the JDK installation directory.
- Check the project's Java compiler level after creating the project.
If Eclipse opens but reports that no compatible Java runtime is available, confirm that the installed Eclipse release supports your JDK and that Eclipse is configured to use the intended JDK.
5. How to Create a Java Project in Eclipse
For this tutorial, create a Maven project. Maven manages libraries for us, so we do not have to download each JAR file and add it manually.
- Open Eclipse and choose a workspace.
- Select File → New → Maven Project. If the option is not visible, use File → New → Other and search for Maven.
- For a simple setup, select Create a simple project (skip archetype selection), then click Next.
- Enter a Group Id, such as
com.way2testing. - Enter an Artifact Id, such as
rest-assured-demo. - Choose a suitable version and packaging type
jar, then finish the wizard. - Open the generated
pom.xmlfile.
What are Group Id and Artifact Id? The Group Id identifies the organization or project group, while the Artifact Id is the name of this particular project or build artifact. Together they help Maven identify the project.
Simplified view of the project components you will use.
6. Introduction to Maven and pom.xml
Apache Maven is a build and dependency-management tool for Java projects. It can download libraries, compile code, run tests, and package the project using a consistent set of commands.
What is pom.xml?
pom.xml means Project Object Model. It is the main Maven configuration file. It describes the project, its dependencies, build plugins, and other settings.
For example, when you add REST Assured as a dependency, Maven downloads the library and its required dependencies from configured repositories. Eclipse's Maven integration can then add them to the project's build path.
Important Maven terms
- Dependency: A library your project uses.
- Repository: A location where Maven retrieves dependencies and plugins.
- Lifecycle: A sequence of build phases such as compile, test and package.
- Plugin: A component that adds build functionality, such as running JUnit tests.
7. How to Add REST Assured Dependencies to Maven
Open pom.xml and add the dependencies inside the existing <dependencies> element. If your project does not have one yet, add it within the top-level <project> element.
The following example uses REST Assured 5.5.6 and JUnit Jupiter 5.12.2 as an illustrative pinned setup. Before publishing or using this in a new project, check Maven Central and your organization's compatibility requirements for current stable versions.
<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
https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.way2testing</groupId>
<artifactId>rest-assured-demo</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.release>17</maven.compiler.release>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<junit.version>5.12.2</junit.version>
<rest-assured.version>5.5.6</rest-assured.version>
</properties>
<dependencies>
<dependency>
<groupId>io.rest-assured</groupId>
<artifactId>rest-assured</artifactId>
<version>${rest-assured.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.5.2</version>
</plugin>
</plugins>
</build>
</project>
maven.compiler.release must be supported by the JDK used to run Maven. If your environment uses Java 21, you may set the release to 21. Check the selected library and plugin versions against the official release information before using them in production.Refresh Maven dependencies in Eclipse
- Save
pom.xml. - Right-click the project.
- Select Maven → Update Project (often shown as Update Project…).
- Select the project and confirm. Wait for Maven to download dependencies.
If Eclipse reports unresolved dependencies, check your internet/proxy settings and the Maven Console. A red error marker can also mean that the project compiler level and installed JDK do not match.
8. REST Assured Dependencies Explained
| Dependency | Purpose | Why it is included |
|---|---|---|
io.rest-assured:rest-assured | Provides the REST Assured request and response API. | Used to send HTTP requests and assert response details. |
org.junit.jupiter:junit-jupiter | Provides the JUnit 5 testing framework. | Supplies annotations such as @Test and test execution support. |
maven-surefire-plugin | Runs tests during Maven's test phase. | Allows mvn test to discover and execute compatible tests. |
Why use test scope? These libraries are required for testing but normally should not be packaged as application runtime dependencies. Maven's test scope makes them available to test compilation and execution.
Can I use TestNG instead of JUnit?
Yes. REST Assured works with multiple Java test frameworks. If your team uses TestNG, add the TestNG dependency and a compatible Maven Surefire configuration, then use TestNG's @Test annotation. Do not copy both sets of test annotations into the same beginner example unless you intentionally want to demonstrate both frameworks.
9. Understanding the Maven Project Structure
A standard Maven project separates production code from test code. For an API testing project, you will usually place test classes under src/test/java.
rest-assured-demo/
├── pom.xml
├── src/
│ ├── main/
│ │ └── java/
│ │ └── (optional reusable production code)
│ └── test/
│ ├── java/
│ │ └── com/
│ │ └── way2testing/
│ │ └── GetUserTest.java
│ └── resources/
│ └── (optional test data and configuration)
└── target/
└── (generated build and test output)
src/main/java: application or reusable main Java code, if needed.src/test/java: automated test classes.src/test/resources: test data and configuration files.pom.xml: Maven project configuration and dependencies.target: files generated by Maven; do not normally edit these manually.
In Eclipse, Maven source folders may be displayed with package names rather than the complete physical folder path. Create the package com.way2testing under src/test/java, then create a class named GetUserTest.
10. Your First REST Assured API Test
Now create a test that sends a GET request and verifies the response. To make this example runnable without depending on a third-party public service, the test uses https://jsonplaceholder.typicode.com/users/1, a public demo API. Its availability and response may change, so use it for learning rather than production tests.
Step 1: Create the test class
In Eclipse, right-click src/test/java → New → Package, create com.way2testing, then right-click that package → New → Class. Name it GetUserTest.
Step 2: Add this code
package com.way2testing;
import static io.restassured.RestAssured.given;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.notNullValue;
import org.junit.jupiter.api.Test;
class GetUserTest {
@Test
void shouldReturnUserDetails() {
given()
.accept("application/json")
.when()
.get("https://jsonplaceholder.typicode.com/users/1")
.then()
.statusCode(200)
.contentType("application/json; charset=utf-8")
.body("id", equalTo(1))
.body("name", notNullValue());
}
}
Note about Content-Type: Exact response content-type strings can vary by server and library behavior. If the demo API changes its charset or media-type formatting, you can assert only the media type or use the API's documented response contract. The important beginner checks are the status code and expected JSON values.
Step 3: Understand the test
given()starts the request setup.accept("application/json")tells the server that JSON is an acceptable response format.get(...)sends a GET request to the endpoint.statusCode(200)asserts that the server returned HTTP 200.body("id", equalTo(1))checks that the JSON response has anidvalue of 1.body("name", notNullValue())checks that the name field is not null.
11. Understanding given(), when() and then()
REST Assured's fluent syntax groups an API test into three readable stages:
The basic REST Assured test flow.
given() — Request setup
Configure headers, query parameters, path parameters, authentication, cookies, and request bodies.
given()
.header("Accept", "application/json")
.queryParam("active", true)
when() — Perform the action
Call the required HTTP method such as get(), post(), put(), patch(), or delete().
when()
.get("https://api.example.com/users")
then() — Validate the result
Assert the status code, headers, response body, and other relevant results.
then()
.statusCode(200)
.body("data.size()", equalTo(3));
The endpoint and expected data in the short snippets above are illustrative. A test should use assertions that match the actual API contract and test data.
12. How to Execute REST Assured Tests from Eclipse
- Save
pom.xmland wait for Maven dependencies to finish resolving. - Confirm that the test class is under
src/test/javaand imports JUnit 5 classes. - Right-click
GetUserTest.java. - Select Run As → JUnit Test.
- Review the JUnit view for the test result and the Console for additional output.
A green success indicator means the assertions passed. A failure indicator means at least one assertion or another part of the test failed. Read the exception and actual response before changing the assertion.
If “Run As → JUnit Test” is missing
- Check that the JUnit dependency has downloaded successfully.
- Make sure the test class is in
src/test/java. - Confirm the
@Testimport isorg.junit.jupiter.api.Testfor this JUnit 5 example. - Use Maven → Update Project and refresh the project if necessary.
13. How to Run Tests Using Maven
Maven lets you run tests outside the IDE, which is useful for repeatable local runs and later CI/CD integration.
Run tests from Eclipse
- Right-click the project.
- Select Run As → Maven test or Run As → Maven Build…, depending on the Eclipse installation.
- If using Maven Build, enter
testin the Goals field and run it.
Run tests from a terminal
Open a terminal in the folder containing pom.xml and execute:
mvn test
To force Maven to check for updated dependencies, you can use:
mvn -U test
The -U flag asks Maven to check for updated snapshots and missing releases according to repository rules; it is not needed for every run.
Useful Maven commands
| Command | Purpose |
|---|---|
mvn -version | Shows Maven and Java information. |
mvn clean | Removes the generated build output directory. |
mvn test | Compiles and runs tests in the test phase. |
mvn clean test | Cleans previous build output and runs tests again. |
mvn -q test | Runs tests with quieter Maven console output; test failures are still reported. |
Test reports and detailed results are usually written under target/surefire-reports. The exact files depend on the test framework and Maven configuration.
14. Common Installation and Dependency Errors and Their Solutions
Error 1: 'java' is not recognized
Likely cause: Java is not installed, or the JDK bin folder is missing from PATH.
Solution: Verify the JDK installation, configure JAVA_HOME and %JAVA_HOME%\bin, then open a new terminal and run java -version.
Error 2: 'javac' is not recognized
Likely cause: A JRE-only setup is being used, or PATH points to the wrong directory.
Solution: Install a JDK and verify that %JAVA_HOME%\bin contains javac.exe.
Error 3: JAVA_HOME points to an invalid directory
Likely cause: The variable contains a typo, points to a nonexistent folder, or includes \bin when the tool expects the JDK root.
Solution: Copy the actual JDK installation directory into JAVA_HOME. Do not include quotes unless a particular tool explicitly requires them.
Error 4: Maven dependency cannot be resolved
Likely cause: No internet connection, proxy configuration, a repository outage, an incorrect dependency coordinate, or a version that does not exist in the configured repository.
Solution: Check the Maven Console, confirm the group ID/artifact ID/version, check repository and proxy settings, and retry after connectivity is restored.
Error 5: Unsupported class file version or Java compiler error
Likely cause: The JDK running Maven, Eclipse's configured JDK, and the project's compiler release do not match.
Solution: Check java -version, mvn -version, Eclipse Installed JREs, and maven.compiler.release. Align them to a supported version.
Error 6: No tests were executed
Likely cause: The test class is not in the expected source folder, the class or method naming does not match discovery rules, the wrong test annotation was imported, or the test runner configuration is incompatible.
Solution: Put tests under src/test/java, verify the JUnit 5 import, confirm Surefire compatibility, and inspect the Maven output for test discovery messages.
Error 7: API test fails with a connection or timeout error
Likely cause: The API is unreachable, the endpoint is incorrect, network access is restricted, a proxy is required, or the demo service is temporarily unavailable.
Solution: Try the endpoint using an approved API client, verify the URL and network settings, and check the service status. Avoid treating a network outage as an assertion failure in the API's business logic.
Error 8: Expected status code differs from actual status code
Likely cause: The API contract, test data, authentication, or server response differs from the assumption in the test.
Solution: Read the actual status and response body, confirm the endpoint documentation, and update the test only if the expected behavior was incorrect.
Summary and Next Tutorial
You have now learned how to prepare a Java API testing environment, configure Java and Eclipse, create a Maven project, add REST Assured dependencies, understand the project structure, and write a basic GET API test using JUnit 5.
Before moving on, confirm that you can run mvn test and see your test result. If that works, your environment is ready for the next lesson.
Practice Tasks
- Install a JDK and confirm that both
java -versionandjavac -versionwork. - Create a Maven project named
rest-assured-demoin Eclipse. - Add the REST Assured and JUnit dependencies to
pom.xml. - Run the sample GET test from Eclipse.
- Run the same test using
mvn clean test. - Change the expected ID from
1to2. Observe the assertion failure and understand the actual-versus-expected output. - Restore the correct assertion and run the test again.
No comments:
Post a Comment