Skip to content
This repository was archived by the owner on Apr 4, 2025. It is now read-only.

Commit 8db8978

Browse files
committed
Fix reactive tests to properly complete.
Use proper verification procedures. Resolves #48
1 parent a37b863 commit 8db8978

File tree

1 file changed

+49
-46
lines changed

1 file changed

+49
-46
lines changed

src/test/java/org/springframework/session/data/mongo/ReactiveMongoOperationsSessionRepositoryTest.java

+49-46
Original file line numberDiff line numberDiff line change
@@ -62,47 +62,46 @@ public class ReactiveMongoOperationsSessionRepositoryTest {
6262
private MongoOperations blockingMongoOperations;
6363

6464
@Before
65-
public void setUp() throws Exception {
65+
public void setUp() {
6666

6767
this.repository = new ReactiveMongoOperationsSessionRepository(this.mongoOperations);
6868
this.repository.setMongoSessionConverter(this.converter);
6969
}
7070

7171
@Test
72-
public void shouldCreateSession() throws Exception {
72+
public void shouldCreateSession() {
7373

74-
// when
75-
Mono<MongoSession> session = this.repository.createSession();
76-
77-
// then
78-
StepVerifier.create(session)
74+
this.repository.createSession()
75+
.as(StepVerifier::create)
7976
.expectNextMatches(mongoSession -> {
8077
assertThat(mongoSession.getId()).isNotEmpty();
8178
assertThat(mongoSession.getMaxInactiveInterval().getSeconds())
8279
.isEqualTo(ReactiveMongoOperationsSessionRepository.DEFAULT_INACTIVE_INTERVAL);
8380
return true;
84-
});
81+
})
82+
.verifyComplete();
8583
}
8684

8785
@Test
88-
public void shouldCreateSessionWhenMaxInactiveIntervalNotDefined() throws Exception {
86+
public void shouldCreateSessionWhenMaxInactiveIntervalNotDefined() {
8987

9088
// when
9189
this.repository.setMaxInactiveIntervalInSeconds(null);
92-
Mono<MongoSession> session = this.repository.createSession();
9390

9491
// then
95-
StepVerifier.create(session)
92+
this.repository.createSession()
93+
.as(StepVerifier::create)
9694
.expectNextMatches(mongoSession -> {
9795
assertThat(mongoSession.getId()).isNotEmpty();
9896
assertThat(mongoSession.getMaxInactiveInterval().getSeconds())
9997
.isEqualTo(ReactiveMongoOperationsSessionRepository.DEFAULT_INACTIVE_INTERVAL);
10098
return true;
101-
});
99+
})
100+
.verifyComplete();
102101
}
103102

104103
@Test
105-
public void shouldSaveSession() throws Exception {
104+
public void shouldSaveSession() {
106105

107106
// given
108107
MongoSession session = new MongoSession();
@@ -115,16 +114,15 @@ public void shouldSaveSession() throws Exception {
115114
given(this.mongoOperations.save(dbSession, "sessions")).willReturn(Mono.just(dbSession));
116115

117116
// when
118-
StepVerifier.create(this.repository.save(session))
119-
.expectNextMatches(aVoid -> {
120-
// then
121-
verify(this.mongoOperations).save(dbSession, ReactiveMongoOperationsSessionRepository.DEFAULT_COLLECTION_NAME);
122-
return true;
123-
});
117+
this.repository.save(session)
118+
.as(StepVerifier::create)
119+
.verifyComplete();
120+
121+
verify(this.mongoOperations).save(dbSession, ReactiveMongoOperationsSessionRepository.DEFAULT_COLLECTION_NAME);
124122
}
125123

126124
@Test
127-
public void shouldGetSession() throws Exception {
125+
public void shouldGetSession() {
128126

129127
// given
130128
String sessionId = UUID.randomUUID().toString();
@@ -139,16 +137,14 @@ public void shouldGetSession() throws Exception {
139137
TypeDescriptor.valueOf(MongoSession.class))).willReturn(session);
140138

141139
// when
142-
StepVerifier.create(this.repository.findById(sessionId))
143-
.expectNextMatches(retrievedSession -> {
144-
// then
145-
assertThat(retrievedSession).isEqualTo(session);
146-
return true;
147-
});
140+
this.repository.findById(sessionId)
141+
.as(StepVerifier::create)
142+
.expectNext(session)
143+
.verifyComplete();
148144
}
149145

150146
@Test
151-
public void shouldHandleExpiredSession() throws Exception {
147+
public void shouldHandleExpiredSession() {
152148

153149
// given
154150
String sessionId = UUID.randomUUID().toString();
@@ -157,43 +153,50 @@ public void shouldHandleExpiredSession() throws Exception {
157153
given(this.mongoOperations.findById(sessionId, Document.class,
158154
ReactiveMongoOperationsSessionRepository.DEFAULT_COLLECTION_NAME)).willReturn(Mono.just(sessionDocument));
159155

156+
given(this.mongoOperations.remove(sessionDocument, ReactiveMongoOperationsSessionRepository.DEFAULT_COLLECTION_NAME))
157+
.willReturn(Mono.just(DeleteResult.acknowledged(1)));
158+
160159
MongoSession session = mock(MongoSession.class);
161160

162161
given(session.isExpired()).willReturn(true);
163162
given(this.converter.convert(sessionDocument, TypeDescriptor.valueOf(Document.class),
164163
TypeDescriptor.valueOf(MongoSession.class))).willReturn(session);
165164

166165
// when
167-
StepVerifier.create(this.repository.findById(sessionId))
168-
.expectNextMatches(mongoSession -> {
169-
// then
170-
verify(this.mongoOperations).remove(any(Document.class),
171-
eq(ReactiveMongoOperationsSessionRepository.DEFAULT_COLLECTION_NAME));
172-
return true;
173-
});
166+
this.repository.findById(sessionId)
167+
.as(StepVerifier::create)
168+
.verifyComplete();
174169

170+
// then
171+
verify(this.mongoOperations).remove(any(Document.class),
172+
eq(ReactiveMongoOperationsSessionRepository.DEFAULT_COLLECTION_NAME));
175173
}
176174

177175
@Test
178-
public void shouldDeleteSession() throws Exception {
176+
public void shouldDeleteSession() {
179177

180178
// given
181179
String sessionId = UUID.randomUUID().toString();
182-
183180
Document sessionDocument = new Document();
184181

185-
given(this.mongoOperations.findById(eq(sessionId), eq(Document.class),
186-
eq(ReactiveMongoOperationsSessionRepository.DEFAULT_COLLECTION_NAME))).willReturn(Mono.just(sessionDocument));
187-
given(this.mongoOperations.remove((Mono<? extends Object>) any(), eq("sessions"))).willReturn(Mono.just(DeleteResult.acknowledged(1)));
182+
given(this.mongoOperations.findById(sessionId, Document.class,
183+
ReactiveMongoOperationsSessionRepository.DEFAULT_COLLECTION_NAME)).willReturn(Mono.just(sessionDocument));
184+
185+
given(this.mongoOperations.remove(sessionDocument, "sessions"))
186+
.willReturn(Mono.just(DeleteResult.acknowledged(1)));
187+
188+
MongoSession session = mock(MongoSession.class);
189+
190+
given(this.converter.convert(sessionDocument, TypeDescriptor.valueOf(Document.class),
191+
TypeDescriptor.valueOf(MongoSession.class))).willReturn(session);
188192

189193
// when
190-
StepVerifier.create(this.repository.deleteById(sessionId))
191-
.expectNextMatches(aVoid -> {
192-
// then
193-
verify(this.mongoOperations).remove(any(Document.class),
194-
eq(ReactiveMongoOperationsSessionRepository.DEFAULT_COLLECTION_NAME));
195-
return true;
196-
});
194+
this.repository.deleteById(sessionId)
195+
.as(StepVerifier::create)
196+
.verifyComplete();
197+
198+
verify(this.mongoOperations).remove(any(Document.class),
199+
eq(ReactiveMongoOperationsSessionRepository.DEFAULT_COLLECTION_NAME));
197200
}
198201

199202
@Test

0 commit comments

Comments
 (0)