0
0
JUnittesting~8 mins

assertSame and assertNotSame in JUnit - Framework Patterns

Choose your learning style9 modes available
Framework Mode - assertSame and assertNotSame
Folder Structure of a JUnit Test Automation Framework
project-root/
├── src/
│   ├── main/
│   │   └── java/
│   │       └── com/example/app/          # Application source code
│   └── test/
│       └── java/
│           └── com/example/app/tests/    # JUnit test classes
│               ├── unit/                  # Unit tests
│               ├── integration/           # Integration tests
│               └── utils/                 # Test utilities
├── resources/
│   └── test-data/                         # Test data files
├── build.gradle or pom.xml                # Build and dependency management
└── junit-platform.properties              # JUnit configuration
  
Test Framework Layers
  • Test Classes: Contain JUnit test methods using assertions like assertSame and assertNotSame to verify object references.
  • Test Utilities: Helper methods for creating test objects or mocks to support assertions.
  • Configuration Layer: Manages test environment setup, e.g., test profiles or system properties.
  • Build & Dependency Layer: Handles JUnit and other dependencies via Maven or Gradle.
Configuration Patterns
  • JUnit Platform Properties: Use junit-platform.properties to configure test discovery and execution.
  • Profiles: Use Maven or Gradle profiles to run tests in different environments.
  • System Properties: Pass JVM arguments to configure tests dynamically.
  • Test Data: Store test data in resources/test-data for reuse in tests.
Test Reporting and CI/CD Integration
  • JUnit Reports: Generate XML and HTML reports automatically via Maven Surefire or Gradle Test tasks.
  • CI/CD Integration: Integrate with Jenkins, GitHub Actions, or GitLab CI to run tests on code commits.
  • Assertion Outcomes: assertSame passes if two references point to the same object; fails otherwise. assertNotSame passes if references differ.
  • Failure Logs: Include clear messages showing expected and actual object references for debugging.
Framework Design Principles for assertSame and assertNotSame
  • Use assertSame to check object identity: Verify that two variables point to the exact same object instance.
  • Use assertNotSame to ensure distinct objects: Confirm that two references do not point to the same object.
  • Write clear assertion messages: Help understand failures by adding descriptive messages to assertions.
  • Keep tests isolated: Avoid shared mutable state that can cause false positives or negatives in identity assertions.
  • Organize tests by purpose: Separate tests that check object identity from those that check object equality.
Self-Check Question

Where in this folder structure would you add a new test class that uses assertSame to verify singleton instances?

Key Result
JUnit test framework organizes tests in src/test/java with clear layers for tests, utilities, and configuration, using assertSame/assertNotSame to verify object identity.