-
Notifications
You must be signed in to change notification settings - Fork 0
/
MethodLoggingAspectTest.kt
102 lines (83 loc) · 3.04 KB
/
MethodLoggingAspectTest.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
package io.fixture.aspect
import ch.qos.logback.classic.Level
import ch.qos.logback.classic.spi.ILoggingEvent
import ch.qos.logback.core.read.ListAppender
import io.fixture.aspect.test.LoggedMethodClass
import kotlin.test.assertTrue
import org.junit.After
import org.junit.Before
import org.junit.Test
class MethodLoggingAspectTest {
val appender = ListAppender<ILoggingEvent>()
val oldLevel = LoggedMethodClass.logger.getLevel()
[Before]
fun setUp() {
appender.setContext(LoggedMethodClass.logger.getLoggerContext())
appender.start()
LoggedMethodClass.logger.addAppender(appender)
LoggedMethodClass.logger.setLevel(Level.TRACE)
}
[After]
fun tearDown() {
appender.stop()
appender.list.clear()
LoggedMethodClass.logger.setLevel(oldLevel)
LoggedMethodClass.logger.detachAppender(appender)
}
[Test]
fun testAfterReturning() {
LoggedMethodClass().doSomething("message", "value")
assertTrue("a message with a return value should be logged") {
appender.list.any {
it.getMessage() == "doSomething(message=message, object=value) - end - returning: value"
}
}
}
[Test]
fun testAfterReturningWithoutReturnType() {
LoggedMethodClass().doNothing("message", "value")
assertTrue("a message without a return value should be logged") {
appender.list.any {
it.getMessage() == "doNothing(message=message, object=value) - end"
}
}
}
[Test]
fun testAfterThrowing() {
try {
LoggedMethodClass().doSomething("message", RuntimeException("Test Exception"))
}
catch (e: RuntimeException) {
// Need to catch it here, rather than in Test(expected=) so we can assert below
}
assertTrue("a message with an exception should be logged") {
appender.list.any {
it.getMessage() == "doSomething(message=message, object=java.lang.RuntimeException: Test Exception) - end - throwing: RuntimeException"
}
}
}
[Test]
fun testAfterThrowingWithoutLogging() {
LoggedMethodClass.logger.setLevel(Level.OFF)
try {
LoggedMethodClass().doSomething("message", RuntimeException("Test Exception"))
}
catch (e: RuntimeException) {
// Need to catch it here, rather than in Test(expected=) so we can assert below
}
assertTrue("a message with an exception should not be logged") {
appender.list.all {
it.getMessage() != "doSomething(message=message, object=java.lang.RuntimeException: Test Exception) - end - throwing: RuntimeException"
}
}
}
[Test]
fun testBefore() {
LoggedMethodClass().doSomething("message", "value")
assertTrue("a message should be logged") {
appender.list.any {
it.getMessage() == "doSomething(message=message, object=value) - start"
}
}
}
}