11package com .baeldung .si ;
22
3- import static org .junit .Assert .* ;
3+ import static org .junit .Assert .assertEquals ;
44
5+ import org .hamcrest .core .IsInstanceOf ;
6+ import org .junit .Rule ;
57import org .junit .Test ;
8+ import org .junit .rules .ExpectedException ;
69import org .junit .runner .RunWith ;
710import org .springframework .beans .factory .annotation .Autowired ;
811import org .springframework .messaging .SubscribableChannel ;
2124@ ContextConfiguration (classes = { SecurityConfig .class , SecuredDirectChannel .class , MessageConsumer .class })
2225public class TestSpringIntegrationSecurity {
2326
27+ @ Rule
28+ public ExpectedException expectedException = ExpectedException .none ();
29+
2430 @ Autowired
2531 SubscribableChannel startDirectChannel ;
2632
@@ -34,34 +40,28 @@ public void givenNoUser_whenSendToDirectChannel_thenCredentialNotFound() {
3440 startDirectChannel .send (new GenericMessage <String >(DIRECT_CHANNEL_MESSAGE ));
3541 }
3642
37- @ Test ( expected = AccessDeniedException . class )
43+ @ Test
3844 @ WithMockUser (username = "jane" , roles = { "LOGGER" })
39- public void givenRoleLogger_whenSendMessageToDirectChannel_thenAccessDenied () throws Throwable {
40- try {
41- startDirectChannel .send (new GenericMessage <String >(DIRECT_CHANNEL_MESSAGE ));
42- } catch (Exception e ) {
43- throw e .getCause ();
44- }
45+ public void givenRoleLogger_whenSendMessageToDirectChannel_thenAccessDenied () {
46+ expectedException .expectCause (IsInstanceOf .<Throwable > instanceOf (AccessDeniedException .class ));
47+
48+ startDirectChannel .send (new GenericMessage <String >(DIRECT_CHANNEL_MESSAGE ));
4549 }
4650
47- @ Test ( expected = AccessDeniedException . class )
51+ @ Test
4852 @ WithMockUser (username = "jane" )
49- public void givenJane_whenSendMessageToDirectChannel_thenAccessDenied () throws Throwable {
50- try {
51- startDirectChannel .send (new GenericMessage <String >(DIRECT_CHANNEL_MESSAGE ));
52- } catch (Exception e ) {
53- throw e .getCause ();
54- }
53+ public void givenJane_whenSendMessageToDirectChannel_thenAccessDenied () {
54+ expectedException .expectCause (IsInstanceOf .<Throwable > instanceOf (AccessDeniedException .class ));
55+
56+ startDirectChannel .send (new GenericMessage <String >(DIRECT_CHANNEL_MESSAGE ));
5557 }
5658
57- @ Test ( expected = AccessDeniedException . class )
59+ @ Test
5860 @ WithMockUser (roles = { "VIEWER" })
59- public void givenRoleViewer_whenSendToDirectChannel_thenAccessDenied () throws Throwable {
60- try {
61- startDirectChannel .send (new GenericMessage <String >(DIRECT_CHANNEL_MESSAGE ));
62- } catch (Exception e ) {
63- throw e .getCause ();
64- }
61+ public void givenRoleViewer_whenSendToDirectChannel_thenAccessDenied () {
62+ expectedException .expectCause (IsInstanceOf .<Throwable > instanceOf (AccessDeniedException .class ));
63+
64+ startDirectChannel .send (new GenericMessage <String >(DIRECT_CHANNEL_MESSAGE ));
6565 }
6666
6767 @ Test
@@ -78,4 +78,4 @@ public void givenJaneLoggerEditor_whenSendToDirectChannel_thenFlowCompleted() {
7878 assertEquals (DIRECT_CHANNEL_MESSAGE , messageConsumer .getMessageContent ());
7979 }
8080
81- }
81+ }
0 commit comments