KEMBAR78
Software Testing and JUnit and Best Practices | PPTX
Software Testing
Tung Thanh Nguyen, PhD
Department of Computer Science
Fulbright University Vietnam
Introduction to Software Testing
• Software testing ensures software quality
• Detecting bugs and verifying functionality.
• Validating that software meets requirements and behaves as expected.
• JUnit is a popular testing framework for Java applications.
Types of Software Testing
• Manual Testing - Performed by humans without automation.
• Automated Testing - Uses software tools to run tests automatically.
• Unit Testing - Tests individual components of code.
• Integration Testing - Verifies interactions between components.
• System Testing - Tests all system’s functionality.
• User Acceptance Testing - Validates software requirements from user
perspective.
Junit – Testing framrwork for Java
• Provides a structured way to write and execute tests.
• Supports test automation.
• Integrates well with build tools (e.g., Maven, Gradle).
• Helps ensure code reliability and maintainability.
Installing JUnit
• Add JUnit dependency to Maven:
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.7.1</version>
<scope>test</scope>
</dependency>
Writing a Basic JUnit Test
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
class CalculatorTest {
@Test
void testAddition() {
assertEquals(5, 2 + 3);
}
}
JUnit Annotations
• @Test - Marks a test method.
• @BeforeEach - Runs before each test.
• @AfterEach - Runs after each test.
• @BeforeAll - Runs once before all tests.
• @AfterAll - Runs once after all tests.
Setup and Teardown Methods
import org.junit.jupiter.api.*;
class TestLifecycle {
@BeforeEach
void setUp() {
System.out.println("Setting up test");
}
@AfterEach
void tearDown() {
System.out.println("Cleaning up test");
}
@Test
void exampleTest() {
assertTrue(true);
}
}
Testing Exceptions
import static org.junit.jupiter.api.Assertions.*;
class ExceptionTest {
@Test
void testException() {
assertThrows(ArithmeticException.class, () -> {
int result = 10 / 0;
});
}
}
Parameterized Tests
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
class ParameterizedExample {
@ParameterizedTest
@ValueSource(ints = {1, 2, 3})
void testNumbers(int number) {
assertTrue(number > 0);
}
}
Test Coverage Metrics
• Statement Coverage - Tests if each line is executed.
• Branch Coverage - Ensures all branches in control structures execute.
• Path Coverage - Tests all possible paths in a function.
Assertions in JUnit
import static org.junit.jupiter.api.Assertions.*;
class AssertionsExample {
@Test
void testAssertions() {
assertEquals(4, 2 + 2);
assertNotNull("Hello");
assertTrue(5 > 3);
}
}
Running JUnit Tests
• Use mvn test for Maven projects.
• Use gradle test for Gradle projects.
• Run tests directly in IDE (e.g., IntelliJ, Eclipse).
Continuous Integration with JUnit
• Use Jenkins, GitHub Actions, or GitLab CI/CD.
• Automate test execution on code commits.
Handling Test Failures
• Check error messages and stack traces.
• Debug using breakpoints.
• Improve test reliability with proper setup.
Best Practices in Unit Testing
• Keep tests independent.
• Use meaningful test names.
• Cover edge cases.
• Run tests frequently.
Summary
• Software testing is crucial for code quality.
• JUnit simplifies unit testing.
• Use assertions and CI/CD for robust testing.

Software Testing and JUnit and Best Practices

  • 1.
    Software Testing Tung ThanhNguyen, PhD Department of Computer Science Fulbright University Vietnam
  • 2.
    Introduction to SoftwareTesting • Software testing ensures software quality • Detecting bugs and verifying functionality. • Validating that software meets requirements and behaves as expected. • JUnit is a popular testing framework for Java applications.
  • 3.
    Types of SoftwareTesting • Manual Testing - Performed by humans without automation. • Automated Testing - Uses software tools to run tests automatically. • Unit Testing - Tests individual components of code. • Integration Testing - Verifies interactions between components. • System Testing - Tests all system’s functionality. • User Acceptance Testing - Validates software requirements from user perspective.
  • 4.
    Junit – Testingframrwork for Java • Provides a structured way to write and execute tests. • Supports test automation. • Integrates well with build tools (e.g., Maven, Gradle). • Helps ensure code reliability and maintainability.
  • 5.
    Installing JUnit • AddJUnit dependency to Maven: <dependency> <groupId>org.junit.jupiter</groupId> <artifactId>junit-jupiter-api</artifactId> <version>5.7.1</version> <scope>test</scope> </dependency>
  • 6.
    Writing a BasicJUnit Test import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.*; class CalculatorTest { @Test void testAddition() { assertEquals(5, 2 + 3); } }
  • 7.
    JUnit Annotations • @Test- Marks a test method. • @BeforeEach - Runs before each test. • @AfterEach - Runs after each test. • @BeforeAll - Runs once before all tests. • @AfterAll - Runs once after all tests.
  • 8.
    Setup and TeardownMethods import org.junit.jupiter.api.*; class TestLifecycle { @BeforeEach void setUp() { System.out.println("Setting up test"); } @AfterEach void tearDown() { System.out.println("Cleaning up test"); } @Test void exampleTest() { assertTrue(true); } }
  • 9.
    Testing Exceptions import staticorg.junit.jupiter.api.Assertions.*; class ExceptionTest { @Test void testException() { assertThrows(ArithmeticException.class, () -> { int result = 10 / 0; }); } }
  • 10.
    Parameterized Tests import org.junit.jupiter.params.ParameterizedTest; importorg.junit.jupiter.params.provider.ValueSource; class ParameterizedExample { @ParameterizedTest @ValueSource(ints = {1, 2, 3}) void testNumbers(int number) { assertTrue(number > 0); } }
  • 11.
    Test Coverage Metrics •Statement Coverage - Tests if each line is executed. • Branch Coverage - Ensures all branches in control structures execute. • Path Coverage - Tests all possible paths in a function.
  • 12.
    Assertions in JUnit importstatic org.junit.jupiter.api.Assertions.*; class AssertionsExample { @Test void testAssertions() { assertEquals(4, 2 + 2); assertNotNull("Hello"); assertTrue(5 > 3); } }
  • 13.
    Running JUnit Tests •Use mvn test for Maven projects. • Use gradle test for Gradle projects. • Run tests directly in IDE (e.g., IntelliJ, Eclipse).
  • 14.
    Continuous Integration withJUnit • Use Jenkins, GitHub Actions, or GitLab CI/CD. • Automate test execution on code commits.
  • 15.
    Handling Test Failures •Check error messages and stack traces. • Debug using breakpoints. • Improve test reliability with proper setup.
  • 16.
    Best Practices inUnit Testing • Keep tests independent. • Use meaningful test names. • Cover edge cases. • Run tests frequently.
  • 17.
    Summary • Software testingis crucial for code quality. • JUnit simplifies unit testing. • Use assertions and CI/CD for robust testing.