What is Cucumber Framework?

  

Cucumber is one of the most popular tools used for Behavior Driven Development (BDD). It allows developers, testers, and business analysts to write test cases in a natural, human-readable language that bridges the gap between technical and non-technical stakeholders.


What is Cucumber ?

Cucumber is an open-source BDD testing tool written in Ruby, but it supports many languages like Java, JavaScript, Python, and others. It enables you to write executable specifications for software behavior using a plain text language called Gherkin.

These Gherkin scenarios are then mapped to step definitions, which are actual automation code written in a programming language like Java or Python.


Why Use Cucumber?

  • Encourages collaboration between developers, testers, and business users.

  • Helps in defining clear acceptance criteria.

  • Makes test cases more understandable to non-technical stakeholders.

  • Acts as documentation of the system’s behavior.

  • Supports automation and manual readability together.


Example of Cucumber in BDD

Gherkin Feature File - login.feature

Feature: Login Functionality

  Scenario: Successful login with valid credentials
    Given the user is on the login page
    When the user enters a valid username and password
    And clicks on the login button
    Then the user should be redirected to the homepage



Step Definition file - Java class

@Given("the user is on the login page")
public void user_on_login_page() {
    // Code to open login page
}

@When("the user enters a valid username and password")
public void enter_credentials() {
    // Code to enter username and password
}

@When("clicks on the login button")
public void click_login_button() {
    // Code to click login button
}

@Then("the user should be redirected to the homepage")
public void redirect_to_homepage() {
    // Code to verify homepage
}




Features of Cucumber

FeatureDescription
Gherkin LanguageUses plain English-like syntax (Given-When-Then) to write scenarios.
Supports Multiple LanguagesWorks with Java, Ruby, Python, JavaScript, etc.
Readable by Non-Technical UsersDesigned to be easily understood by business stakeholders.
Integration with Selenium and Other ToolsWorks well with automation tools like Selenium, Appium.
ReusabilityStep definitions can be reused across multiple scenarios.
TagsHelps in grouping and selectively running scenarios.
HooksAllows setting up @Before and @After actions for scenarios.
ReportsSupports various report formats like HTML, JSON, etc.
Parallel ExecutionAllows running scenarios or features in parallel for faster testing.
Live DocumentationFeature files serve as live documentation of the system.


Advantages of Cucumber

  • Readable and easy to write test scenarios.

  • Encourages collaboration among teams.

  • Facilitates automated acceptance testing.

  • Promotes test-first approach in Agile and BDD.


Disadvantages of Cucumber

  • May add overhead for small projects.

  • Requires learning curve for writing Gherkin properly.

  • May lead to duplicate steps if not managed well.

  • Execution is slower compared to unit test frameworks.

Maven Build Automation

  


What is Build Automation in Maven ?

Build Automation in Maven refers to the automatic handling of the build lifecycle—compiling code, running tests, packaging, installing, and deployingespecially when multiple projects depend on one another.


Maven and Project Dependencies

In many real-world applications, software is split into modules or multiple Maven projects, where one project (child) depends on another (parent or dependency).


For example:

Project A (Core Library)
Project B (Depends on A)


When you change and build Project AProject B should automatically be rebuilt using the updated version of A. Maven handles this automation.


How Maven Automates Builds for Dependent Projects

1. Using mvn install to Install Artifacts

  • When you run mvn install in Project A, Maven:

    • Compiles the source code.

    • Packages it (JAR/WAR).

    • Installs it into the local repository (~/.m2/repository).

  • Now, any project like Project B, which depends on A, can fetch this artifact from the local repository.



2. Declaring Dependencies in pom.xml

Project B’s pom.xml declares:


<dependencies>
  <dependency>
    <groupId>com.example</groupId>
    <artifactId>project-a</artifactId>
    <version>1.0-SNAPSHOT</version>
  </dependency>
</dependencies>



Maven will now:

  • Automatically fetch and include Project A’s build.

  • Recompile Project B if Project A is updated and reinstalled.



Multi-Module Project Automation (Advanced)

If Projects A and B are part of a multi-module Maven project, Maven will:

  • Automatically build modules in the correct order based on dependencies.

  • Avoid needing manual builds/install for each module.


Parent pom.xml:

<modules>
  <module>project-a</module>
  <module>project-b</module>
</modules>


Maven will:

  • Build project-a first.

  • Then build project-b after project-a is available.




Benefits of Maven Build Automation for Dependencies


BenefitDescription
EfficiencyRebuilds only what's changed.
Dependency ManagementAutomatically pulls dependent artifacts.
ConsistencyEnsures consistent versioning across builds.
Local/Remote Repo UseBuilds can pull from local/remote repositories.
Tool IntegrationWorks with Jenkins, CI/CD, IDEs, etc.



Example Workflow

  • Modify code in Project A.
  • Run mvn install in Project A → Installs updated JAR in local repo.
  • Run mvn clean install in Project B → Maven automatically picks updated A.
  • All dependent builds are consistent and automated.

Maven Commands

  

Common Maven Commands Explained (Including for Eclipse IDE)

Maven is a powerful build automation tool for Java projects. Whether you're using Maven via the command line or integrated with Eclipse IDE, the core commands remain the same. Here's a breakdown:


Common Maven Commands (Command Line & Eclipse IDE)


CommandDescription
mvn cleanDeletes the target/ directory. Ensures a clean build.
mvn compileCompiles the Java source code in the src/main/java directory.
mvn testRuns unit tests in src/test/java.
mvn packagePackages compiled code into a .jar or .war file.
mvn installInstalls the .jar or .war to your local Maven repository (~/.m2).
mvn deployDeploys the artifact to a remote repository (e.g., Nexus, Artifactory).
mvn siteGenerates project documentation (using plugins).
mvn validateValidates the project structure and checks for missing info in pom.xml.
mvn verifyRuns additional checks like integration tests.
mvn dependency:treeShows the project dependency tree. Helps find conflicts.
mvn help:effective-pomShows the fully resolved POM (with inheritance and profiles applied).
mvn archetype:generateCreates a new Maven project from an archetype (template).


Using Maven Commands in Eclipse IDE

Eclipse IDE (with M2E Plugin—Maven integration) allows you to run Maven commands without using the terminal.


To Run Maven Goals in Eclipse:

  1. Right-click on your Maven project in Project Explorer.

  2. Choose Run As > Maven build...

  3. In the Goals field, enter one or more of the following:

    • clean

    • compile

    • package

    • install

    • test

    • site

  4. Click Run.



After adding dependency in pom.xml, we should update project, as shown below:

  • Right-click → Maven → Update Project to refresh dependencies.


We can also execute maven commands from command line from project workspace.

go to project workspace and open cmd terminal and execute below command:
mvn clean install





Snapshots in Maven

 

In Maven, a snapshot refers to a development version of a project. It indicates that the version is still under development and not yet stable or final.


What Is a Snapshot?

A snapshot is a special version of a project that changes frequently. It is typically used during development before a stable release is made.



Snapshot Version Example:

<version>1.0-SNAPSHOT</version>

Here, 1.0-SNAPSHOT means it is a work-in-progress version of 1.0.



Where Are Snapshots Stored?

Maven uses separate repositories for snapshots and releases:

  • Snapshot repository: https://repo.maven.apache.org/maven2-snapshots (or a custom one)

  • Release repository: https://repo.maven.apache.org/maven2





When to Use Snapshot

  • While developing and testing a new feature.

  • Sharing unstable or work-in-progress code with teammates.

  • Before doing a stable release.




How to Release a Snapshot

When development is complete:

  • Finalize code.
  • Change version from 1.0-SNAPSHOT to 1.0.
  • Deploy to the release repository.

Maven Project Templates

  

Maven Project Templates

Maven project templates are predefined project structures or archetypes that help you quickly generate a new Maven project with a standard setup. These templates are known in Maven as archetypes.


What is a Maven Archetype?

Maven archetype is a template toolkit that allows you to generate a new Maven project with:

  • Standard folder structure (src/main/javasrc/test/java, etc.)

  • A basic pom.xml

  • Sample source and test files (if provided)

  • Optional configurations (example: web app setup)




Commonly Used Maven Archetypes


ArchetypeDescription
maven-archetype-quickstartBasic Java project with one class and one test class
maven-archetype-webappWeb application project with web.xml and web folder structure
maven-archetype-j2ee-simpleSimple J2EE project setup
maven-archetype-siteFor generating a project documentation website
maven-archetype-pluginTemplate for creating a Maven plugin
maven-archetype-portletFor creating Java portlet applications



How to Use a Maven Template (Archetype)

Use the following command to create a new Maven project with a specific archetype:


mvn archetype:generate -DgroupId=com.example \
                       -DartifactId=my-app \
                       -DarchetypeArtifactId=maven-archetype-quickstart \
                       -DinteractiveMode=false





Explanation:

  • groupId: The project group name (like a package)

  • artifactId: The project name

  • archetypeArtifactId: The template to use

  • interactiveMode=false: Skip the interactive prompts




Benefits of Using Maven Archetypes

  • Saves time setting up boilerplate

  • Enforces standard structure and conventions

  • Useful for both beginners and experienced developers

  • Supports team consistency

Project Documentation in Maven

  

To generate project documentation in Maven, the standard way is to use the Maven Site Plugin. This plugin helps generate a complete site (including Javadoc, dependency info, reports, etc.) for your project.


Step-by-Step Guide

1. Add Site Plugin (optional for customization)

Maven includes the Site plugin by default, but you can customize it in pom.xml:


<build>
  <plugins>
    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-site-plugin</artifactId>
      <version>4.0.0-M14</version> <!-- Latest version as of mid-2024 -->
    </plugin>
  </plugins>
</build>



2. Add Reporting Section (for documentation reports)

This section includes things like javadocsurefire-report, etc.


<reporting>
  <plugins>
    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-project-info-reports-plugin</artifactId>
    </plugin>

    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-javadoc-plugin</artifactId>
    </plugin>

    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-surefire-report-plugin</artifactId>
    </plugin>
  </plugins>
</reporting>


3. Generate the Site

Use the following Maven command:

mvn site



This will:

  • Compile the code

  • Generate reports (Javadoc, tests, etc.)

  • Create an HTML site under the target/site directory



4. View the Site

Open the following file in a browser:

target/site/index.html





Common Reports Generated:


Report NameDescription
Project SummaryOverview of the project
JavaDocAPI documentation from source code comments
Surefire ReportTest results
Dependency InfoDependencies and dependency tree
SCM & Issue TrackingInfo from pom.xml

How to add Maven Dependencies in Pom.xml

 

In Maven, adding external dependencies means telling Maven which libraries your project needs, so it can automatically download them from a remote repository (like Maven Central) and include them in your build.


 What is a Dependency in Maven?

dependency is an external Java library (JAR file) your project needs to compile or run. You declare dependencies in the pom.xml file.


How to Add External Dependencies in Maven?

1. Locate Dependency Info

You need to know:

  • groupId

  • artifactId

  • version

You can find these on https://mvnrepository.com



2. Add Dependency to pom.xml


<dependencies>
  <dependency>
    <groupId>com.google.code.gson</groupId>
    <artifactId>gson</artifactId>
    <version>2.10.1</version>
  </dependency>
</dependencies>




Maven will:

  • Download it from Maven Central

  • Store it in your local .m2 repository

  • Add it to your project classpath





 Example: Adding Multiple Dependencies


<dependencies>
  <dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-lang3</artifactId>
    <version>3.12.0</version>
  </dependency>

  <dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>4.13.2</version>
    <scope>test</scope>
  </dependency>
</dependencies>

Maven Plugins

 

What are Maven Plugins?

Maven plugins are the core of the build system in Apache Maven. They are used to execute specific tasks/goals during the build lifecycle, such as compiling code, running tests, packaging the application, generating documentation, deploying artifacts, and more.



Examples of Common Maven Plugins:


Plugin NamePurpose
maven-compiler-pluginCompiles Java source code
maven-surefire-pluginRuns unit tests
maven-jar-pluginBuilds a JAR file from the compiled code
maven-war-pluginPackages the project as a WAR file for web applications
maven-deploy-pluginDeploys the final package to a remote repository
maven-clean-pluginCleans up the target directory
maven-install-pluginInstalls the package to the local repository
maven-site-pluginGenerates a project website with reports and documentation
maven-checkstyle-pluginChecks code against a coding standard




Structure of a Plugin in pom.xml:

Here’s how a typical Maven plugin is configured:


<build>
  <plugins>
    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-compiler-plugin</artifactId>
      <version>3.11.0</version>
      <configuration>
        <source>1.8</source>
        <target>1.8</target>
      </configuration>
    </plugin>
  </plugins>
</build>




Goals and Lifecycle Phases:

Each plugin contains one or more goals, and goals are typically bound to build lifecycle phases like:


PhaseTypical Plugin Goals Bound
compilecompiler:compile
testsurefire:test
packagejar:jar or war:war
installinstall:install
deploydeploy:deploy



You can also run a plugin goal directly using the command:

mvn plugin-prefix:goal


Example:


mvn compiler:compile



Why Use Plugins?

  • Automate repetitive tasks

  • Ensure consistent build process

  • Customize build according to project needs

  • Integrate external tools like Checkstyle, PMD, SpotBugs

  • Generate documentation, reports, and more


Custom Plugins:

You can also create your own Maven plugin by implementing the org.apache.maven.plugin.AbstractMojo class and defining your plugin's goals.



At a glance:


FeatureDescription
What is a plugin?A collection of goals used in Maven builds
PurposeAutomate build tasks: compile, test, package, deploy, etc.
Configuration locationInside the <build><plugins> section of pom.xml
Built-in examplesmaven-compiler-plugin, maven-surefire-plugin, maven-jar-plugin, etc
Custom pluginYes, developers can create custom plugins

How to Build and Test in Maven

 


How to Build and Test in Maven?

Maven is a build automation and dependency management tool used primarily for Java projects. It uses a file called pom.xml (Project Object Model) to manage builds, dependencies, plugins, and more.



1. Building a Maven Project

Steps to Build:

Step 1: Install Maven

mvn -version



Step 2: Navigate to Project Directory


cd path/to/your/project




Step 3: Run Build Commands

Common Maven Build Commands:



CommandDescription
mvn validateValidate project structure and configuration
mvn compileCompile the Java source code
mvn testRun unit tests using JUnit/TestNG
mvn packageCompile + bundle code into a JAR/WAR file
mvn installInstall package to local .m2 repository
mvn cleanDelete the target/ folder (cleans old builds)
mvn clean installClean → compile → test → package → install



2. Testing in Maven

Maven runs tests using Surefire Plugin, and the test classes must follow these conventions:

Write a Unit Test


Example with JUnit:


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

public class CalculatorTest {
    @Test
    public void testAddition() {
        Calculator calc = new Calculator();
        assertEquals(5, calc.add(2, 3));
    }
}




Run Tests


mvn test




Skip Tests


mvn install -DskipTests




Run Specific Test


mvn -Dtest=CalculatorTest test




Plugins That Help Build & Test


PluginPurpose
maven-compiler-pluginCompiles Java code
maven-surefire-pluginRuns unit tests
maven-failsafe-pluginRuns integration tests
maven-jar-pluginPackages into .jar
maven-war-pluginPackages into .war (for web apps)



Example:


<build>
  <plugins>
    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-compiler-plugin</artifactId>
      <version>3.8.1</version>
      <configuration>
        <source>11</source>
        <target>11</target>
      </configuration>
    </plugin>
  </plugins>
</build>

Maven Repositories

  

What is a Maven Repository?

Maven repository is a storage location where Maven artifacts (like JAR files, plugins, and other dependencies) are stored and managed. These artifacts are required for building Java projects using Maven.

Repositories play a key role in dependency management, allowing Maven to download required libraries automatically, rather than developers having to manually include them.



Types of Maven Repositories

Maven repositories are generally classified into three types:



Repository TypeDescription
Local RepositoryA folder on the developer’s computer where Maven stores downloaded dependencies from remote repositories.
Central RepositoryA default, public repository provided by Maven community at https://repo.maven.apache.org/maven2. It contains a vast collection of open-source libraries.
Remote RepositoryAny custom repository hosted on a server, typically within an organization or on the internet (e.g., JFrog Artifactory, Nexus). Used when artifacts are not available in the central repo.



1. Local Repository

  • Location: ~/.m2/repository (default location on your machine).

  • Maven first checks the local repository before looking elsewhere.

  • If a dependency is not found locally, it is fetched from a remote repository and stored locally for future use.



2. Central Repository

  • Provided by the Maven community.

  • Maven refers here if the dependency is not found in the local repo.

  • URL: https://repo.maven.apache.org/maven2

  • No configuration is needed — it's used by default.



3. Remote Repository

  • Maintained by organizations to host private or custom-built artifacts.

  • Can be configured in pom.xml or settings.xml:


<repositories>
  <repository>
    <id>my-org-repo</id>
    <url>https://repo.mycompany.com/maven2</url>
  </repository>
</repositories>



How Maven Uses Repositories During Build:

  • Check local repository for required dependencies.
  • If not found, go to the configured remote repositories or central repository.
  • Download the dependency and store it in the local repository.
  • Use it to compile and build the project.



Points to Remember:
  • repository is like a warehouse for Java libraries.

  • Helps automate dependency management.

  • Reduces manual work by automatically downloading and resolving dependencies.