Class DefaultMockitoSessionBuilder

    • Field Detail

      • testClassInstances

        private final java.util.List<java.lang.Object> testClassInstances
      • name

        private java.lang.String name
    • Constructor Detail

      • DefaultMockitoSessionBuilder

        public DefaultMockitoSessionBuilder()
    • Method Detail

      • initMocks

        public MockitoSessionBuilder initMocks​(java.lang.Object testClassInstance)
        Description copied from interface: MockitoSessionBuilder
        Adds the test class instance for initialization of fields annotated with Mockito annotations like Mock. When this method is invoked it does not perform initialization of mocks on the spot! Only when MockitoSessionBuilder.startMocking() is invoked then annotated fields will be initialized. Traditional API to initialize mocks, the MockitoAnnotations.openMocks(Object) method has limited support for driving cleaner tests because it does not support configuring Strictness. Want cleaner tests and better productivity? Migrate from MockitoAnnotations.openMocks(Object) to MockitoSession!

        This method may be called multiple times to add multiple, e.g. nested, test class instances.

        See code sample in MockitoSession.

        Specified by:
        initMocks in interface MockitoSessionBuilder
        Parameters:
        testClassInstance - test class instance that contains fields with Mockito annotations to be initialized. Passing null is permitted but will be ignored.
        Returns:
        the same builder instance for fluent configuration of MockitoSession.
      • initMocks

        public MockitoSessionBuilder initMocks​(java.lang.Object... testClassInstances)
        Description copied from interface: MockitoSessionBuilder
        Adds the test class instances for initialization of fields annotated with Mockito annotations like Mock.

        In most scenarios, you only need to init mocks on a single test class instance. This method is useful for advanced framework integrations (like JUnit Jupiter), when a test uses multiple, e.g. nested, test class instances.

        This method calls MockitoSessionBuilder.initMocks(Object) for each passed test class instance.

        Specified by:
        initMocks in interface MockitoSessionBuilder
        Parameters:
        testClassInstances - test class instances that contains fields with Mockito annotations to be initialized. Passing null or an empty array is permitted but will be ignored.
        Returns:
        the same builder instance for fluent configuration of MockitoSession.
        See Also:
        MockitoSessionBuilder.initMocks(Object)
      • name

        public MockitoSessionBuilder name​(java.lang.String name)
        Description copied from interface: MockitoSessionBuilder
        Configures the name of the MockitoSession instance.

        The name is used to output hints when finishing a session.

        This method is intended to be used by framework integrations, e.g. JUnit. When building a MockitoSession for direct use, users are not expected to call it.

        Specified by:
        name in interface MockitoSessionBuilder
        Parameters:
        name - of MockitoSession instance. Passing null is permitted and will make the session use a default value. The current default is the name of the last test class instance passed to MockitoSessionBuilder.initMocks(Object) or MockitoSessionBuilder.initMocks(Object...), if available; otherwise, "<Unnamed Session>" is used.
        Returns:
        the same builder instance for fluent configuration of MockitoSession.
        See Also:
        MockitoHint
      • logger

        public MockitoSessionBuilder logger​(MockitoSessionLogger logger)
        Description copied from interface: MockitoSessionBuilder
        Configures logger used by MockitoSession for emitting warnings when finishing the session.

        Please note that the use of strict stubs is recommended over emitting warnings because warnings are easily ignored and spoil the log output. Instead of using this method, please consider setting strictness with MockitoSessionBuilder.strictness(Strictness).

        Specified by:
        logger in interface MockitoSessionBuilder
        Parameters:
        logger - for warnings emitted when finishing MockitoSession. Passing null is permitted and will make the session use a default value. By default, warnings will be logged to the console.
        Returns:
        the same builder instance for fluent configuration of MockitoSession.
        See Also:
        MockitoHint