aboutsummaryrefslogtreecommitdiffstats
path: root/cps-service/src
diff options
context:
space:
mode:
authoremaclee <lee.anjella.macabuhay@est.tech>2022-07-13 17:11:51 +0100
committeremaclee <lee.anjella.macabuhay@est.tech>2022-07-13 17:51:47 +0100
commite338a80e47aede5374d0127e844f915b12242776 (patch)
treeef59b3f592e6419585e0d13f4e9d07a0d3938601 /cps-service/src
parentbef15a680d6cf10acea2ddb52b281284f8a83247 (diff)
Refactor unit test for Notification Error Handler
Issue-ID: CPS-1146 Signed-off-by: emaclee <lee.anjella.macabuhay@est.tech> Change-Id: Idd1c1236b9574c1d070c3dc4111ebcce8d800b23
Diffstat (limited to 'cps-service/src')
-rw-r--r--cps-service/src/test/groovy/org/onap/cps/notification/NotificationErrorHandlerSpec.groovy31
1 files changed, 24 insertions, 7 deletions
diff --git a/cps-service/src/test/groovy/org/onap/cps/notification/NotificationErrorHandlerSpec.groovy b/cps-service/src/test/groovy/org/onap/cps/notification/NotificationErrorHandlerSpec.groovy
index 4cacc43656..d0cd47383f 100644
--- a/cps-service/src/test/groovy/org/onap/cps/notification/NotificationErrorHandlerSpec.groovy
+++ b/cps-service/src/test/groovy/org/onap/cps/notification/NotificationErrorHandlerSpec.groovy
@@ -19,23 +19,40 @@
package org.onap.cps.notification
+import ch.qos.logback.classic.Logger
+import ch.qos.logback.classic.spi.ILoggingEvent
+import ch.qos.logback.core.read.ListAppender
+import org.junit.jupiter.api.AfterEach
+import org.junit.jupiter.api.BeforeEach
+import org.slf4j.LoggerFactory
+
import spock.lang.Specification
class NotificationErrorHandlerSpec extends Specification{
NotificationErrorHandler objectUnderTest = new NotificationErrorHandler()
+ def logWatcher = Spy(ListAppender<ILoggingEvent>)
+
+ @BeforeEach
+ void setup() {
+ ((Logger) LoggerFactory.getLogger(NotificationErrorHandler.class)).addAppender(logWatcher);
+ logWatcher.start();
+ }
+
+ @AfterEach
+ void teardown() {
+ ((Logger) LoggerFactory.getLogger(NotificationErrorHandler.class)).detachAndStopAllAppenders();
+ }
def 'Logging exception via notification error handler'() {
- given: 'redirect system.out to a readable stream'
- def systemOutAsStream = new ByteArrayOutputStream()
- System.out = new PrintStream(systemOutAsStream)
when: 'some exception occurs'
objectUnderTest.onException(new Exception('sample exception'), 'some context')
then: 'log output results contains the correct error details'
- def systemOutAsString = systemOutAsStream.toString()
- systemOutAsString.contains('Failed to process')
- systemOutAsString.contains('Error cause: sample exception')
- systemOutAsString.contains('Error context: [some context]')
+ def logMessage = logWatcher.list.get(0).getFormattedMessage()
+ logMessage.contains(
+ "Failed to process \n" +
+ " Error cause: sample exception \n" +
+ " Error context: [some context]")
}
}