Cucumber and Rest-Assured need Java to be installed on the system to run the tests. Click here to learn How to install Java.
Step 2 – Download and setup Eclipse IDE on the system
The Eclipse IDE (integrated development environment) provides strong support for Java developers. Click here to learn How to install Eclipse.
Step 3 – Setup Gradle
To build a test framework, we need to add several dependencies to the project. This can be achieved by any build tool. I have used the Gradle Build Tool. Click here to learn How to install Gradle.
Step 4 – Create a new Gradle Project
To create a new Gradle project, go to the top left side and select File -> New Project -> Gradle -> Gradle project -> Next ->Enter Project Name and Project Location ->Next ->Select Gradle Version ->Next ->Review the Configuration -> Finish.
Step 5 – Add Rest-Assured and Cucumber dependencies to the Gradle project
This syntax is used for Gradle 5.0 and higher.
dependencies {
// This dependency is exported to consumers, that is to say found on their compile classpath.
api 'org.apache.commons:commons-math3:3.6.1'
// This dependency is used internally, and not exposed to consumers on their own compile classpath.
implementation 'com.google.guava:guava:29.0-jre'
testImplementation 'io.cucumber:cucumber-java:6.8.1'
testImplementation 'io.cucumber:cucumber-junit:6.8.1'
testImplementation 'io.rest-assured:rest-assured:4.3.3'
If you are using Gradle 4.10.3 or older, use the below dependency block to build.gradle.
Once you have added dependencies, configurations, and Gradle cucumber task, Right-click on the project, Hover to the Gradle option, and click Refresh Gradle Project. Eclipse does not automatically update the classpath of the build.gradle file is updated. Select GradleRefresh Gradle Project from the context menu of the project or from your build.gradle file for that.
Step 8 – Create a feature file under src/test/resources
A new Gradle Project is created with 4 folders – src/main/java, src/main/resources, src/test/java and src/test/resources. Features are created under the src/test/resources directory. Create a folder with name features. Now, create the feature file in this folder. The feature file should be saved with the extension .feature. This feature file contains the test scenarios created to test the application. The Test Scenarios are written in Gherkins language in the format of Given, When, Then, And, But.
Below is an example of a Test Scenario where we are using the GET method to get the information from the API.
Feature: Validation of get method
@GetUserDetails
Scenario Outline: Send a valid Request to get user details
Given I send a request to the URL to get user details
Then the response will return status 200 and id <id> and salary <employee_salary> and name "<employee_name>" and age <employee_age> and message "<message>"
Examples:
|id |employee_salary|employee_name |employee_age |message |
|1 |320800 |Tiger Nixon |61 |Successfully! Record has been fetched. |
Step 9 – Create the Step Definition class or Glue Code for the Test Scenario
Step Definition acts as an intermediate to your runner and feature file. It stores the mapping between each step of the scenario in the Feature file. So when you run the scenario, it will scan the step definition file to check the matched glue or test code.
import io.restassured.http.ContentType;
import io.restassured.response.ValidatableResponse;
import static io.restassured.RestAssured.given;
import static org.hamcrest.Matchers.equalTo;
import io.cucumber.java.en.Given;
import io.cucumber.java.en.Then;
public class API_GETDefinitions {
private ValidatableResponse validatableResponse;
private String endpoint = "http://dummy.restapiexample.com/api/v1/employee/1";
@Given("I send a request to the URL to get user details")
public void sendRequest(){
validatableResponse = given().contentType(ContentType.JSON)
.when().get(endpoint).then();
System.out.println("Response :"+validatableResponse.extract().asPrettyString());
}
@Then("the response will return status {int} and id {int} and salary {int} and name {string} and age {int} and message {string}")
public void verifyStatus(int statusCode, int id, int emp_Salary, String emp_name, int emp_age, String message ){
validatableResponse.assertThat().statusCode(statusCode);
validatableResponse.assertThat().body("data.id",equalTo(id));
validatableResponse.assertThat().body("data.employee_salary",equalTo(emp_Salary));
validatableResponse.assertThat().body("data.employee_name",equalTo(emp_name));
validatableResponse.assertThat().body("data.employee_age",equalTo(emp_age));
validatableResponse.assertThat().body("message",equalTo(message));
}
}
In order to use REST assured effectively, it’s recommended to statically import methods from the following classes:
A runner will help us to run the feature file and act as an interlink between the feature file and step definition Class. To know more about Runner, refer to this link.
import org.junit.runner.RunWith;
import io.cucumber.junit.Cucumber;
import io.cucumber.junit.CucumberOptions;
@RunWith(Cucumber.class)
@CucumberOptions(plugin ="pretty",features= {"src/test/resources/features/API_GET.feature"}, glue= {"com.example.gradle.apidemo"})
public class CucumberRunnerTest {
}
Step 11 – Run the tests from JUnit
You can execute the test script by right-clicking on the Test Runner class -> Run As JUnit.
Step 12 – Run the tests from the Command Line
Run the following Gradle task from the directory path wherebuild.gradlefile is located. To know more about this report, refer here.
gradle cucumber
Below is the screenshot of the execution of Cucumber tests in Command-Line.
Step 13 – Cucumber Report Generation
To get Cucumber Test Reports, add cucumber.properties under src/test/resources and add the below instruction in the file.
cucumber.publish.enabled=true
Below is the image of the Cucumber Report.
We are done! Congratulations on making it through this tutorial and hope you found it useful! Happy Learning!!
In this tutorial, I will explain Parallel Testing usingCucumber with JUnit4.
Cucumber-JVM allows parallel execution across multiple threads since version 4.0.0. There are several options to incorporate this built-in feature in a Cucumber project. You can do so by using JUnit, TestNG, or CLI.
Cucumber can be executed in parallel using JUnit and Maven test execution plugins.
In JUnit, thefeature files are run in parallel rather than in scenarios, which means all the scenarios in a feature file will be executed by the same thread. You can use either Maven Surefire or Failsafe plugin to execute the runner. In this tutorial, I’m using the Maven Surefire plugin.
Create a Maven project in your favorite IDE using the cucumber-archetype or by adding Cucumber dependencies to the POM as detailed here and Junit dependencies here. To know more about How to set up a Cucumber Maven project with Eclipse, please refer to this tutorial – Cucumber Tutorial – How to setup Cucumber with Eclipse.
Below is the structure of the project.
Step 2 – Update the Properties section in Maven pom.xml
Step 5 – Create a feature folder in src/test/resources
Add 2 feature files – LoginPage.feature and ForgotPasswordPage.feature in the features folder present in src/test/resources.
LoginPage.feature
Feature: Login to HRM Application
Background:
Given User is on Home page
@ValidCredentials
Scenario: Login with valid credentials - Feature 1, Scenario - 1
When User enters username as "Admin" and password as "admin123"
Then User should be able to login successfully
@InvalidCredentials
Scenario Outline: Login with invalid credentials - Feature 1, Scenario - 2
When User enters username as "<username>" and password as "<password>"
Then User should be able to see error message "<errorMessage>"
Examples:
| username | password | errorMessage |
| Admin | admin12$$ | Invalid credentials |
| admin$$ | admin123 | Invalid credentials |
| abc123 | xyz$$ | Invalid credentials |
| | xyz$$ | Invalid credentials |
ForgotPasswordPage.feature
Feature: Forgot Password Page
Background:
Given User is on Home page
@BackFunctionality
Scenario: Validate the cancel functionality - Feature 2, Scenario - 1
When User clicks on Forgot your password? link
Then User should be able to navigate to Reset Password page
And User clicks on Cancel button to go back to Login Page
@ResetFunctionality
Scenario: Validate the Reset Password functionality - Feature 2, Scenario - 2
When User clicks on Forgot your password? link
Then User should be able to navigate to Reset Password page
And User clicks on Reset Password button and provide username as "abc1234"
And Verify the message "Reset Password link sent successfully"
Step 6 – Create the Page Object Model classes of LoginPage and ForgotPasswordPage feature files
Page Object Model class contains all the locators and the actions performed on these locators for the particular class to improve the readability and maintainability of the code.
Below are the Page Object Model classes for these feature files.
LoginPage
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
public class LoginPage {
public WebDriver driver;
By userName = By.name("username");
By passWord = By.name("password");
By login = By.xpath("//*[@id='app']/div[1]/div/div[1]/div/div[2]/div[2]/form/div[3]/button");
By errorMessage = By.xpath("//*[@id='app']/div[1]/div/div[1]/div/div[2]/div[2]/div/div[1]/div[1]/p");
By forgotPasswordLink = By.xpath("//*[@id='app']/div[1]/div/div[1]/div/div[2]/div[2]/form/div[4]/p");
By loginPageTitle = By.xpath("//*[@id='app']/div[1]/div/div[1]/div/div[2]/h5");
public LoginPage(WebDriver driver) {
this.driver = driver;
}
public String getErrorMessage() {
return driver.findElement(errorMessage).getText();
}
public void login(String strUserName, String strPassword) {
// Fill user name
driver.findElement(userName).sendKeys(strUserName);
// Fill password
driver.findElement(passWord).sendKeys(strPassword);
// Click Login button
driver.findElement(login).click();
}
// Click on Forgot Password link
public void clickOnForgotPasswordLink() {
driver.findElement(forgotPasswordLink).click();
}
//Get Login Page Title
public String getLoginPageTitle() {
return driver.findElement(loginPageTitle).getText();
}
}
HomePage
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
public class HomePage {
public WebDriver driver;
public HomePage(WebDriver driver) {
this.driver = driver;
}
By homePageUserName = By.xpath("//*[@id='app']/div[1]/div[1]/header/div[1]/div[1]/span/h6");
public String getHomePageText() {
return driver.findElement(homePageUserName).getText();
}
}
ForgotPasswordPage
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
public class ForgotPasswordPage {
WebDriver driver;
By forgotPasswordPageTitle = By.xpath("//*[@id='app']/div[1]/div[1]/div/form/h6");
By cancelBtn = By.xpath("//*[@id='app']/div[1]/div[1]/div/form/div[2]/button[1]");
By resetPasswordBtn = By.xpath("//*[@id='app']/div[1]/div[1]/div/form/div[2]/button[2]");
By userName = By.name("username");
By resetMessage = By.xpath("//*[@id='app']/div[1]/div[1]/div/h6");
public ForgotPasswordPage(WebDriver driver) {
this.driver = driver;
}
// Get the Title of ForgotPage
public String getForgotPageText() {
return driver.findElement(forgotPasswordPageTitle).getText();
}
// Click Cancel Button
public void clickOnCancelBtn() {
driver.findElement(cancelBtn).click();
}
// Click ResetPassword Button
public void clickOnRestPasswordBtn() {
driver.findElement(resetPasswordBtn).click();
}
// Type username in TextBox
public void TypeOnUsernameTextBox(String username) {
driver.findElement(userName).sendKeys(username);
}
// Get Message
public String getRestMessage() {
return driver.findElement(resetMessage).getText();
}
}
PageObjectManager – This class creates the object of all the above-mentioned Page Object Model classes. This an optional class. If you want you can create the objects in StepDefinition class also.
public class PageObjectManager {
public LoginPage loginPage;
public HomePage homePage;
public ForgotPasswordPage forgotPasswordPage;
public WebDriver driver;
public PageObjectManager(WebDriver driver)
{
this.driver = driver;
}
public LoginPage getLoginPage()
{
loginPage= new LoginPage(driver);
return loginPage;
}
public HomePage getHomePage()
{
homePage = new HomePage(driver);
return homePage;
}
public ForgotPasswordPage getForgotPasswordPage()
{
forgotPasswordPage = new ForgotPasswordPage(driver);
return forgotPasswordPage;
}
}
Step 7 – Create the Step Definition classes for both feature files or Glue Code
Below is the Step Definition for LoginPage.feature.
import org.example.pageObjects.HomePage;
import org.example.pageObjects.LoginPage;
import org.example.pageObjects.PageObjectManager;
import org.example.utils.TestSetUp;
import io.cucumber.java.en.Given;
import io.cucumber.java.en.Then;
import io.cucumber.java.en.When;
import org.junit.Assert;
public class LoginPageDefinitions {
TestSetUp setUp;
public PageObjectManager pageObjectManager;
public LoginPage loginPage;
public HomePage homePage;
public LoginPageDefinitions(TestSetUp setUp) {
this.setUp = setUp;
this.loginPage = setUp.pageObjectManager.getLoginPage();
this.homePage= setUp.pageObjectManager.getHomePage();
}
@Given("User is on Home page")
public void loginTest() {
setUp.baseTest.WebDriverManager().get("https://opensource-demo.orangehrmlive.com/");
}
@When("User enters username as {string} and password as {string}")
public void goToHomePage(String userName, String passWord) {
// login to application
loginPage.login(userName, passWord);
// go the next page
}
@Then("User should be able to login successfully")
public void verifyLogin() {
// Verify home page
Assert.assertTrue(homePage.getHomePageText().contains("Dashboard"));
}
@Then("User should be able to see error message {string}")
public void verifyErrorMessage(String expectedErrorMessage) {
// Verify home page
Assert.assertEquals(loginPage.getErrorMessage(),expectedErrorMessage);
}
}
Below is the Step Definition for ForgotPasswordPage.feature.
import io.cucumber.java.en.Then;
import io.cucumber.java.en.When;
import org.example.pageObjects.ForgotPasswordPage;
import org.example.pageObjects.LoginPage;
import org.example.pageObjects.PageObjectManager;
import org.example.utils.TestSetUp;
import org.junit.Assert;
public class ForgotPasswordPageDefinitions {
TestSetUp setUp;
PageObjectManager pageObjectManager;
public LoginPage loginPage;
public ForgotPasswordPage forgotPasswordPage;
public ForgotPasswordPageDefinitions(TestSetUp setUp) {
this.setUp = setUp;
this.loginPage = setUp.pageObjectManager.getLoginPage();
this.forgotPasswordPage = setUp.pageObjectManager.getForgotPasswordPage();
}
@When("User clicks on Forgot your password? link")
public void forgotPasswordLink() {
loginPage.clickOnForgotPasswordLink();
}
@Then("User should be able to navigate to Reset Password page")
public void verifyForgotPasswordPage() {
Assert.assertEquals(forgotPasswordPage.getForgotPageText(),"Reset Password");
}
@Then("User clicks on Cancel button to go back to Login Page")
public void verifyCancelBtn() {
forgotPasswordPage.clickOnCancelBtn();
Assert.assertEquals(loginPage.getLoginPageTitle(),"Login");
}
@Then("User clicks on Reset Password button and provide username as {string}")
public void verifyResetPasswordBtn(String username) {
forgotPasswordPage.TypeOnUsernameTextBox(username);
forgotPasswordPage.clickOnRestPasswordBtn();
}
@Then("Verify the message {string}")
public void verifyMessage(String message) {
// ForgotPasswordPage forgotPasswordPage = setUp.pageObjectManager.getForgotPasswordPage();
Assert.assertEquals(forgotPasswordPage.getRestMessage(),message);
}
}
Step 8 – Create the Hook Class and Dependency Injection class (TestSetUp) and BaseTest class
Below is the code for the ApplicationHook Class.
import io.cucumber.java.After;
import io.cucumber.java.AfterStep;
import io.cucumber.java.Scenario;
import org.example.utils.TestSetUp;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
public class ApplicationHooks {
public TestSetUp setUp;
public ApplicationHooks(TestSetUp setUp) {
this.setUp = setUp;
}
@After
public void tearDown( ) {
setUp.baseTest.WebDriverManager().quit();
}
@AfterStep
public void addScreenshot(Scenario scenario) {
WebDriver driver = setUp.baseTest.WebDriverManager();
if(scenario.isFailed()) {
final byte[] screenshot = ((TakesScreenshot) driver).getScreenshotAs(OutputType.BYTES);
scenario.attach(screenshot, "image/png", "image");
}
}
}
Below is the code for the Dependency Injection class. In Cucumber, if we want to share the state between multiple-step definition files, we will need to use dependency injection (DI).
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import pageObjects.PageObjectManager;
public class TestSetUp {
public WebElement errorMessage;
public WebElement homePageUserName;
public PageObjectManager pageObjectManager;
public BaseTest baseTest;
public TestSetUp() {
baseTest = new BaseTest();
pageObjectManager = new PageObjectManager(baseTest.WebDriverManager());
}
}
BaseTest class is used to initialize the WebDriver.
import io.github.bonigarcia.wdm.WebDriverManager;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import java.time.Duration;
public class BaseTest {
public WebDriver driver;
public final static int TIMEOUT = 10;
public WebDriver WebDriverManager () {
if (driver == null) {
WebDriverManager.chromedriver().setup();
driver = new ChromeDriver();
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(TIMEOUT));
driver.manage().window().maximize();
driver.get(url);
}
return driver;
}
}
Step 9 – Create a Test Runner to run the tests
import io.cucumber.junit.Cucumber;
import io.cucumber.junit.CucumberOptions;
import org.junit.runner.RunWith;
@RunWith(Cucumber.class)
@CucumberOptions(tags = "", features = "src/test/resources/features", glue = "org.example.definitions")
public class CucumberRunnerTests {
}
Step 10 – Cucumber Report Generation
To get Cucumber Test Reports, add cucumber.properties in src/test/resources and add the below instruction in the file.
cucumber.publish.enabled=true
Step 11 – Execute the tests from the command line
mvn clean test
Below is the execution screen. There are two feature files.
When we invoke the test through Maven, the surefire plugin executes the Feature files parallelly. Here, LoginPage has 5 scenarios and ForgotPasswordPage has 2 scenarios. So, initially when the execution will start 1 scenario from both the tests will be executed parallelly and then again one test from each feature will execute. Later, we will be left with 4 scenarios in the LoginPage feature file, so the scenario will run sequentially of the LoginPage feature file.
All the tests of a particular feature file are executed together as feature files are run in parallel, not scenarios.
Step 12 – Difference between Parallel tests and Non-Parallel Tests
Parallel Tests
Below is the Cucumber Report generated for parallel tests.
When the tests are run as JUnit tests from CucumberRunnerTests, then the tests are executed sequentially.
We are done! Congratulations on making it through this tutorial and hope you found it useful! Happy Learning!!
Step 1- Create a Project Folder and change the directory path to the folder where we want to save the latest files created post creation of the project. Here, I have created a folder – GradleIntelliJDemoFromCMD and changed the directory path.
cd C:\Users\Vibha\Projects\Vibha_Personal\GradleIntelliJDemoFromCMD
Step 2 – Open the Terminal in IntelliJ.
Step 3 – Gradle comes with a built-in task, called init , that initializes a new Gradle project in an empty folder. The init task uses the (also built-in) wrapper task to create a Gradle wrapper script, gradlew. Type the below command and press ENTER.
Step 4 – Select the type of project to generate. I’m selecting the application option as if I select basic, it won’t create a src directory. Type 2 and press ENTER.
Step 5 – Select implementation language. This is a Java project, so TYPE 3(Java) and press ENTER.
Step 6 – Select build script DSL (Domain Specific Language) – As in Maven POM.xml (XML) is created to build a script file, here we can use Groovy or Kotlin to build the script file. Type 1 (Groovy) and press ENTER.
Step 7 – Select Test Framework – There are 4 different test frameworks. Depending on your requirement, select an option. I have selected 1 (JUnit 4) and press ENTER.
Step 8 – It needs the Project name and Source Package name. If I won’t provide the project name, it will take by default my current folder name which is Gradle_Project. Similarly, if I won’t provide the Source Package name, then it will provide the current project name as Source Package Name.
Project name – GradleIntelliJDemoFromCMD Source Package – com.example
Press ENTER. init script will run and create a Gradle project. You can see as the build is successfull.
Step 9 – The project is created and placed under the folder GradleIntelliJDemoFromCMD as shown below.
This project structure will have below mentioned files:-
In the previous tutorial, I have explainedhow to create a Java Gradle project in IntelliJ. In this tutorial, I will explain about creating a Java Gradle project Eclipse. I have used Gradle 6.6 to create the project.
Steps to follow:-
Step 1 – To create a new project – Click on the “New” and then select – “Project”.
Step 2 – Select the “Gradle Project”and click on the “Next” button.
Step 3 – A welcome screen will appear. You can uncheck the box – Show the welcome page the next time the wizard appears. This is optional. Click the NEXTbutton.
Step 4 – Below the screen will appear. Mention the “Project Name – GradleEclipseDemo”. Mention the location where we want to save the project in the system. Click the NEXTbutton.
Step 5 – Options screen appear. Make sure you use Gradle version 6.6 to create Gradle project in Eclipse for Version: 2021- 03 (4.19.0).
Note:- If you will try to use version higher than 6.6, then Gradle project structure will have a Gradle project with the nested project with a lib subproject in it.
Step 6 – Verify the Gradle Version and Gradle project structure name.
Step 7 – Below is the structure of Gradle project. The init task generates the new project with the following structure:-
Settings file to define build name and subprojects – settings.gradle
Build script of lib project – build.gradle
Default Java source folder – src/main/java
Default Java test source folder – src/test/java
Step 8– Below is the structure and content of the build.gradle.
/*
* This file was generated by the Gradle 'init' task.
*
* This generated file contains a sample Java Library project to get you started.
* For more details take a look at the Java Libraries chapter in the Gradle
* User Manual available at https://docs.gradle.org/6.6/userguide/java_library_plugin.html
*/
plugins {
// Apply the java-library plugin to add support for Java Library
id 'java-library'
}
repositories {
// Use jcenter for resolving dependencies.
// You can declare any Maven/Ivy/file repository here.
jcenter()
}
dependencies {
// This dependency is exported to consumers, that is to say found on their compile classpath.
api 'org.apache.commons:commons-math3:3.6.1'
// This dependency is used internally, and not exposed to consumers on their own compile classpath.
implementation 'com.google.guava:guava:29.0-jre'
// Use JUnit test framework
testImplementation 'junit:junit:4.13'
}
plugins – Apply the java-library plugin for API and implementation separation.
jcenter – Use JCentral for resolving dependencies. JCenter is a central repository on JFrog Bintray platform for finding and sharing popular JVM language packages in Maven format
api – This dependency is exported to consumers, that is to say found on their compile classpath.
implementation – This dependency is used internally, and not exposed to consumers on their own compile classpath.
testImplementation – Use JUnit test framework.
Step 9 – To check if the project is created successfully. In gradle tasks tab -> navigate to the project -> expand build folder -> right click on build -> Select Run Gradle tasks.
This will be the output of the Gradle Run.
That’s it. We have successfully created a Gradle Java project in Eclipse.
Congratulations on making it through this tutorial and hope you found it useful! Happy Learning!! Cheers!!
In this tutorial, I will explain How to install Gradle on Windows.
Steps to follow:-
Step 1 – To install Gradle on window, we need to download Gradle from Official Gradle Site. I am downloading gradle-8.8.
Step 2 – Create a new directory Documents\Vibha\Automationwith File Explorer.
Step 3 – Copy the extracted files under C:\Users\ykv12\Documents\Vibha\Automation\gradle-8.8-bin. Below is the image of the folder.
Step 4 – We need to configure GRADLE_HOMEenvironment variable. Type – “View Adva” in the search option and we will see the option – View Advanced system setting.
Step 5 – In System Properties dialog, select Advancedtab and click on the Environment Variables button.
Step 6 – In “Environment variables” dialog, System variables, Clicks on the New button and add a GRADLE_HOMEvariable .
Step 7 – Below is the image which shows addition of Environment Variables.
Step 8 – Add %GRADLE_HOME%\bin (full path till bin where gradle is placed on your machine) to Path. Click New Button present in System Variable and add GRADLE_HOME\bin.
How to verify if Gradle is install properly on your machine
Open command prompt and type gradle -version, then the screen should look something like below screen.
That’s it! We have installed Gradle.
Congratulations on making it through this tutorial and hope you found it useful! Happy Learning!! Cheers!!
In the previous tutorial, I have explained How to create a Java Gradle in IntelliJ. This tutorial will explain How to import the Java Gradle project in IntelliJ.
Steps to follow:-
Step 1 – Open IntelliJ IDEA and Welcome Screen appears. Click the Open button present on Welcome Screen.
Step 2 – Navigate to your Gradle project and select the top-level folder. Select the project you want to Import. Select the OK button to proceed to the next screen.
Step 3 – A screen appears to Open or Import project. It will have all the possible configurations for the project. As this is a Gradle project, select Gradle project and click the OK Button.
Step 4 – A warning message box will appear. Select Trust Project button and move forward.
Step 5 – The imported project structure in IntelliJ is shown below.
Step 6 – This screen shows that the project is imported and build successfully.
Step 7 – This screen shows the build.gradle of the imported project.
Step 8 – Run the test present in the project. Here, I have run App. Right-click on App ->Run ‘App.main()’. The below screen shows that the project is imported successfully.
That’s it! We are done!!!
Congratulations on making it through this tutorial and hope you found it useful! Happy Learning!! Cheers!!
Step 1 – Open Eclipse IDE. In Eclipse IDE, select File ->Import ->Maven ->Existing Gradle Project. Click NEXT Button.
Step 2 – A welcome screen will appear. You can uncheck the box – Show the welcome page the next time the wizard appears. This is optional. Click the NEXT button.
Step 3 – Browse the location from where you want to import the Gradle project. Click the NEXT button.
Step 4 – This screen allows us to Configure Customized Workspace Settings. This shows that Gradle Version 7.0 is used for this project. Click the Finish Button.
Step 5 – This screen allows us to review the import configuration. If you feel something is incorrect, click the BACK Button.
Step 6 – We can see that the Gradle project Name is Gradle and other details like Version. Click the FINISH Button. This will import the project structure into Workspace.
Step 7 – Below is the imported project Structure in Eclipse.
Step 8 – To check if the project is imported successfully. Run GradleDemo.java class by right click on the Java class and Run As Java Application.
That’s it! We have imported a Gradle Project in Eclipse.
Congratulations on making it through this tutorial and hope you found it useful! Happy Learning!! Cheers!!
Step 1 – Open Command Prompt. Change current folder to the folder where we want to create the Java project.
cd C:\Users\vibha\eclipse-workspace\Projects\Vibha_Personal\Gradle_Project
Step 2 – Create a Project from Gradle Template. Type the below command and press ENTER.
gradle init
Step 3 – Select the type of project to generate. I’m selecting the application option as if I select basic, it won’t create a src directory. Type 2 and press ENTER.
Step 4 – Select implementation language. This is a Java project, so TYPE 3 and press ENTER.
Step 5 – Select Split functionality across multiple subprojects. I have selected 1 as I want only 1 application. Type 1 and press ENTER.
Step 6 – Select build script DSL (Domain Specific Language) – As in Maven POM.xml (XML) is created to build a script file, here we can use Groovy or Kotlin to build the script file. Type 1 (Groovy) and press ENTER.
Step 7 – Select Test Framework – There are 4 different test frameworks. Depending on your requirement, select an option. I have selected 1 (JUnit 4) and press ENTER.
Step 8 – It needs the Project name and Source Package name. If I won’t provide the project name, it will take by default my current folder name which is Gradle_Project. Similarly, if I won’t provide the Source Package name, then it will provide the current project name as Source Package Name.
Project name – GradleDemoFromCMD Source Package – com.example
Press ENTER. init script will run and create a Gradle project. You can see as the build is successfull.
Step 9 – Below is the project structure. As you can see, the name of the project is GradleDemoFromCMD, but that is not the name of the project folder here. But, when I’ll import this folder into Eclipse, the project name will be GradleDemoFromCMD.
Step 10 – Open app folder. There should be src folder and build.gradle. Open build.gradle.
/*
* This file was generated by the Gradle 'init' task.
*
* This generated file contains a sample Java application project to get you started.
* For more details take a look at the 'Building Java & JVM projects' chapter in the Gradle
* User Manual available at https://docs.gradle.org/7.0/userguide/building_java_projects.html
*/
plugins {
// Apply the application plugin to add support for building a CLI application in Java.
id 'application'
}
repositories {
// Use Maven Central for resolving dependencies.
mavenCentral()
}
dependencies {
// Use JUnit test framework.
testImplementation 'junit:junit:4.13.1'
// This dependency is used by the application.
implementation 'com.google.guava:guava:30.0-jre'
}
application {
// Define the main class for the application.
mainClass = 'com.example.App'
}
This build.gradle contains all the information which I have provided while creating the project.
That’s it! We have created a Gradle Project using Command Line.
Congratulations on making it through this tutorial and hope you found it useful! Happy Learning!! Cheers!!
Step 1 – Open the IntelliJ. It will look as shown below. To create a New Project, click on New Project Icon.
Step 2 – Click on File Option, hover on New Option and click on Project Option as shown below.
Step 3 – Select New Project as Gradle. Project SDK should be current Java version available. Additional Libraries – Java Click on the Next Button.
Step 4 – Below screen will appear. Mention the Name, Group Id, Artifact Id and Version . Click the Finish button
Name : GradleIntelliJDemo Group Id : com.example Artifact Id : GradleIntelliJDemo Version : 1.0-SNAPSHOT
Step 5 – This dialog box will appear on the screen. This provides you the option to open the project in current window or will open a new window with this project. I’m selecting the option – New Window.
Step 6 – This screen shows the structure of Gradle project as well as build.gradle file.
Step 7 – Project Folder Creation – We can see a folder with the name of project – GradleIntelliJDemo in our Eclipse Workspace.
Step 8 – Right click build.gradle and select Run GradleIntelliJDemo. If the build is successful, below screen appears.
This is how we can create the Gradle project – MavenIntelliJDemo in IntelliJ.
Congratulations on making it through this tutorial and hope you found it useful! Happy Learning!! Cheers!!