0
0
JUnittesting~8 mins

Arrange-Act-Assert pattern in JUnit - Framework Patterns

Choose your learning style9 modes available
Framework Mode - Arrange-Act-Assert pattern
Folder Structure
project-root/
├── src/
│   ├── main/
│   │   └── java/
│   │       └── com/example/app/
│   │           └── Calculator.java
│   └── test/
│       └── java/
│           └── com/example/app/
│               └── CalculatorTest.java
└── pom.xml
  

This is a simple Maven project structure for JUnit tests using the Arrange-Act-Assert pattern.

Test Framework Layers
  • Arrange: Prepare test data and environment. For example, create objects or set variables.
  • Act: Execute the method or action under test.
  • Assert: Verify the outcome matches expectations using assertions.
  • Tests: Located in src/test/java, contain test methods following AAA.
  • Application Code: Located in src/main/java, contains the logic to be tested.
Configuration Patterns

JUnit tests typically use Maven or Gradle for build and dependency management.

  • pom.xml: Defines JUnit dependency and test plugins.
  • Test Properties: Use src/test/resources for test-specific config if needed.
  • Environment: Tests run in isolated JVM; environment variables or system properties can be set in test setup if needed.
Test Reporting and CI/CD Integration
  • Maven Surefire plugin generates test reports in target/surefire-reports.
  • Reports show passed/failed tests with stack traces for failures.
  • CI tools (Jenkins, GitHub Actions) run tests automatically and publish reports.
  • Failing tests block merges to keep code quality high.
Best Practices for Arrange-Act-Assert Pattern
  • Keep each test focused on one behavior or scenario.
  • Clearly separate Arrange, Act, and Assert sections with comments or blank lines.
  • Use descriptive test method names to explain what is tested.
  • Avoid logic in tests; tests should be simple and easy to read.
  • Use assertions that give clear failure messages.
Self Check

Where in this framework structure would you add a new test method that uses the Arrange-Act-Assert pattern to verify the addition operation of a Calculator class?

Key Result
Arrange-Act-Assert organizes tests into clear steps: prepare data, perform action, then verify results.