Name: - Md Khalid Roll no.
- 11222584 BCSE-702L (Software Craftsmanship Lab)
EXPERIMENT- 05
AIM: Set up automated tests for a software project and integrate
them into a continuous integration pipeline.
1. Set Up Automated Tests in Java
• Choose a Testing Framework:
o JUnit: The most commonly used testing framework for Java.
o TestNG: Another popular framework with advanced features like
data-driven testing.
• Add Testing Dependencies:
o If you're using Maven, add dependencies to your pom.xml:
Xml
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
<scope>test</scope>
</dependency>
<!-- Add more dependencies if needed -->
</dependencies>
For Gradle, add dependencies to your build.gradle:
Groovy
dependencies {
testImplementation 'junit:junit:4.13.2'
Name: - Md Khalid Roll no.- 11222584 BCSE-702L (Software Craftsmanship Lab)
// Add more dependencies if needed
}
Write Unit Tests:
• Create a test class for each class you want to test, typically
placed in the src/test/java directory.
• Example:
Java
import org.junit.Test;
import static org.junit.Assert.assertEquals;
public class CalculatorTest {
@Test
public void testAdd() {
Calculator calculator = new Calculator();
int result = calculator.add(2, 3);
assertEquals(5, result);
}
}
Write Integration Tests:
• These tests might involve setting up a test database or using
mock services.
• Example using Spring Boot:
Name: - Md Khalid Roll no.- 11222584 BCSE-702L (Software Craftsmanship Lab)
java
@SpringBootTest
public class ApplicationIntegrationTest {
@Autowired
private MockMvc mockMvc;
@Test
public void testHomeController() throws Exception {
mockMvc.perform(get("/"))
.andExpect(status().isOk())
.andExpect(content().string("Welcome"));
}
}
2. Set Up a CI Pipeline for Java
• Choose a CI Service:
o GitHub Actions
o GitLab CI/CD
o Jenkins
o CircleCI
o Travis CI
• Create a CI Configuration File:
o GitHub Actions (.github/workflows/ci.yml):
yaml
Name: - Md Khalid Roll no.- 11222584 BCSE-702L (Software Craftsmanship Lab)
name: Java CI
on: [push, pull_request]
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Set up JDK 11
uses: actions/setup-java@v1
with:
java-version: '11'
- name: Build with Maven
run: mvn clean install
- name: Run tests
run: mvn test
GitLab CI (.gitlab-ci.yml):
Name: - Md Khalid Roll no.- 11222584 BCSE-702L (Software Craftsmanship Lab)
image: maven:3.6.3-jdk-11
stages:
- build
- test
build_job:
stage: build
script:
- mvn clean install
test_job:
stage: test
script:
- mvn test
Jenkins:
• Install the Maven and Git plugins.
• Create a new Freestyle Project or Pipeline.
• Configure your Jenkins job to check out the code from your
repository and run mvn clean install and mvn test.
Travis CI (.travis.yml):
yaml
language: java
Name: - Md Khalid Roll no.- 11222584 BCSE-702L (Software Craftsmanship Lab)
jdk:
- openjdk11
script:
- mvn clean install
- mvn test
• Configure Notifications:
o You can set up email or Slack notifications to be informed
of build failures.
3. Integrate and Monitor
• Push Changes to Your Repository:
o The CI pipeline will automatically trigger on push or pull
request events.
• Monitor the Build and Test Status:
o Use your CI service's dashboard to monitor the status of
your builds and tests.
o If tests fail, investigate and fix the issues.
• Improve Pipeline Over Time:
o Add more tests, including integration and end-to-end
tests.
o Optimize the pipeline for performance (e.g., by caching
dependencies).
4. Maintain and Scale
• Regular Updates:
Name: - Md Khalid Roll no.- 11222584 BCSE-702L (Software Craftsmanship Lab)
o Keep your dependencies, testing tools, and CI
configurations up to date.
• Scaling the CI Pipeline:
o As project grows, consider adding more environments or
stages, such as deployment to a staging environment.