Skip to content

Commit d488e69

Browse files
linhvovnmaibin
authored andcommitted
[BAEL 1382] Update Unit Test (eugenp#3630)
* [tlinh2110-BAEL1382] Add Security in SI * [tlinh2110-BAEL1382] Upgrade to Spring 5 & add Logger * [tlinh2110-BAEL-1382] Update Unit Test
1 parent 3c371be commit d488e69

1 file changed

Lines changed: 23 additions & 23 deletions

File tree

‎spring-integration/src/test/java/com/baeldung/si/TestSpringIntegrationSecurity.java‎

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
package 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;
57
import org.junit.Test;
8+
import org.junit.rules.ExpectedException;
69
import org.junit.runner.RunWith;
710
import org.springframework.beans.factory.annotation.Autowired;
811
import org.springframework.messaging.SubscribableChannel;
@@ -21,6 +24,9 @@
2124
@ContextConfiguration(classes = { SecurityConfig.class, SecuredDirectChannel.class, MessageConsumer.class })
2225
public 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

Comments
 (0)