2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2019 Nordix Foundation.
4 * ================================================================================
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
20 package org.onap.ccsdk.cds.blueprintsprocessor.uat.logging
22 import org.mockito.listeners.InvocationListener
23 import org.mockito.listeners.MethodInvocationReport
24 import org.slf4j.LoggerFactory
25 import org.slf4j.Marker
26 import java.util.concurrent.atomic.AtomicInteger
29 * Logs all Mockito's mock/spy invocations.
31 * Used for debugging interactions with a mock.
33 class MockInvocationLogger(private val marker: Marker) : InvocationListener {
35 private val mockInvocationsCounter = AtomicInteger()
37 override fun reportInvocation(report: MethodInvocationReport) {
38 val sb = StringBuilder()
39 sb.appendln("Method invocation #${mockInvocationsCounter.incrementAndGet()} on mock/spy")
40 report.locationOfStubbing?.let { location ->
41 sb.append(INDENT).append("stubbed ").appendln(location)
43 sb.appendln(report.invocation)
44 sb.append(INDENT).append("invoked ").appendln(report.invocation.location)
45 if (report.threwException()) {
46 sb.append(INDENT).append("has thrown -> ").append(report.throwable.javaClass.name)
47 report.throwable.message?.let { message ->
48 sb.append(" with message ").append(message)
52 sb.append(INDENT).append("has returned -> \"").append(report.returnedValue).append('"')
53 report.returnedValue?.let { value ->
54 sb.append(" (").append(value.javaClass.name).append(')')
58 log.info(marker, sb.toString())
63 private const val INDENT = " "
64 private val log = LoggerFactory.getLogger(MockInvocationLogger::class.java)