Class SessionMock

java.lang.Object
org.apache.maven.api.plugin.testing.stubs.SessionMock

public class SessionMock extends Object
A mock implementation of InternalSession for testing Maven plugins. This class provides a comprehensive mock session that simulates the behavior of a real Maven session, including repository management, artifact handling, and project building capabilities.

The mock session includes pre-configured behaviors for:

  • Repository management (local and remote repositories)
  • Artifact installation and deployment
  • Project building and management
  • Version parsing and handling
  • Model XML processing
  • Session data storage

Example usage in a test:

 
 @Test
 void testMojo() {
     // Create a mock session with a specific local repository
     InternalSession session = SessionMock.getMockSession("/path/to/local/repo");

     // Use the session for testing
     MyMojo mojo = new MyMojo();
     mojo.setSession(session);
     mojo.execute();
 }
 
 

The mock session maintains internal state for:

  • Attached artifacts (via ProjectManager)
  • Artifact paths (via ArtifactManager)
  • System and user properties
  • Session-scoped data (via SessionMock.TestSessionData)

Most service implementations are mocked using Mockito, with pre-configured behaviors that simulate typical Maven operations. Some services, like ModelXmlFactory and VersionParser, use real implementations to ensure correct handling of Maven models and versions.

Since:
4.0.0
See Also: