22
33import org .baeldung .config .TestConfig ;
44import org .baeldung .dao .FooDao ;
5+ import org .baeldung .model .Foo ;
56import org .junit .Before ;
67import org .junit .Test ;
78import org .junit .runner .RunWith ;
1819import java .util .regex .Pattern ;
1920
2021import static org .hamcrest .Matchers .hasSize ;
22+ import static org .hamcrest .core .IsCollectionContaining .hasItem ;
2123import static org .junit .Assert .assertThat ;
2224import static org .junit .Assert .assertTrue ;
2325
@@ -27,6 +29,8 @@ public class AopLoggingTest {
2729
2830 @ Before
2931 public void setUp () {
32+ messages = new ArrayList <>();
33+
3034 logEventHandler = new Handler () {
3135 @ Override
3236 public void publish (LogRecord record ) {
@@ -42,7 +46,8 @@ public void close() throws SecurityException {
4246 }
4347 };
4448
45- messages = new ArrayList <>();
49+ Logger logger = Logger .getLogger (LoggingAspect .class .getName ());
50+ logger .addHandler (logEventHandler );
4651 }
4752
4853 @ Autowired
@@ -54,14 +59,24 @@ public void close() throws SecurityException {
5459
5560 @ Test
5661 public void givenLoggingAspect_whenCallDaoMethod_thenBeforeAdviceIsCalled () {
57- Logger logger = Logger .getLogger (LoggingAspect .class .getName ());
58- logger .addHandler (logEventHandler );
59-
6062 dao .findById (1L );
6163 assertThat (messages , hasSize (1 ));
6264
6365 String logMessage = messages .get (0 );
6466 Pattern pattern = Pattern .compile ("^\\ [\\ d{4}\\ -\\ d{2}\\ -\\ d{2} \\ d{2}:\\ d{2}:\\ d{2}:\\ d{3}\\ ]findById$" );
6567 assertTrue (pattern .matcher (logMessage ).matches ());
6668 }
69+
70+ @ Test
71+ public void givenLoggingAspect_whenCallLoggableAnnotatedMethod_thenMethodIsLogged () {
72+ dao .create (42L , "baz" );
73+ assertThat (messages , hasItem ("Executing method: create" ));
74+ }
75+
76+ @ Test
77+ public void givenLoggingAspect_whenCallMethodAcceptingAnnotatedArgument_thenArgumentIsLogged () {
78+ Foo foo = new Foo (42L , "baz" );
79+ dao .merge (foo );
80+ assertThat (messages , hasItem ("Accepting beans with @Entity annotation: " + foo ));
81+ }
6782}
0 commit comments