All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.mockito.internal.MockedConstructionImpl Maven / Gradle / Ivy

There is a newer version: 5.13.0
Show newest version
/*
 * Copyright (c) 2007 Mockito contributors
 * This program is made available under the terms of the MIT License.
 */
package org.mockito.internal;

import static org.mockito.internal.util.StringUtil.join;

import java.util.Collections;
import java.util.List;

import org.mockito.MockedConstruction;
import org.mockito.exceptions.base.MockitoException;
import org.mockito.internal.debugging.LocationFactory;
import org.mockito.invocation.Location;
import org.mockito.plugins.MockMaker;

public final class MockedConstructionImpl implements MockedConstruction {

    private final MockMaker.ConstructionMockControl control;

    private boolean closed;

    private final Location location = LocationFactory.create();

    protected MockedConstructionImpl(MockMaker.ConstructionMockControl control) {
        this.control = control;
    }

    @Override
    public List constructed() {
        return Collections.unmodifiableList(control.getMocks());
    }

    @Override
    public boolean isClosed() {
        return closed;
    }

    @Override
    public void close() {
        assertNotClosed();

        closed = true;
        control.disable();
    }

    @Override
    public void closeOnDemand() {
        if (!closed) {
            close();
        }
    }

    private void assertNotClosed() {
        if (closed) {
            throw new MockitoException(
                    join(
                            "The static mock created at",
                            location.toString(),
                            "is already resolved and cannot longer be used"));
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy