Skip to content

Commit 4e0457e

Browse files
ngocnhan-tran1996ilayaperumalg
authored andcommitted
Use assertThatException
Signed-off-by: Tran Ngoc Nhan <ngocnhan.tran1996@gmail.com>
1 parent 8620c20 commit 4e0457e

File tree

2 files changed

+10
-24
lines changed

2 files changed

+10
-24
lines changed

spring-ai-model/src/test/java/org/springframework/ai/chat/prompt/PromptTemplateBuilderTests.java

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import org.junit.jupiter.api.Test;
2424

2525
import static org.assertj.core.api.Assertions.assertThat;
26+
import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
2627
import static org.assertj.core.api.Assertions.assertThatThrownBy;
2728

2829
/**
@@ -134,14 +135,8 @@ void builderWithMultipleMissingVariablesShouldThrow() {
134135
.template("Processing {item} with {type} at {level}")
135136
.build();
136137

137-
try {
138-
promptTemplate.render();
139-
Assertions.fail("Expected IllegalStateException was not thrown.");
140-
}
141-
catch (IllegalStateException e) {
142-
assertThat(e.getMessage()).contains("Not all variables were replaced in the template");
143-
assertThat(e.getMessage()).contains("item", "type", "level");
144-
}
138+
assertThatIllegalStateException().isThrownBy(promptTemplate::render)
139+
.withMessageContainingAll("Not all variables were replaced in the template", "item", "type", "level");
145140
}
146141

147142
@Test
@@ -155,13 +150,8 @@ void builderWithPartialVariablesShouldThrow() {
155150
.variables(variables)
156151
.build();
157152

158-
try {
159-
promptTemplate.render();
160-
Assertions.fail("Expected IllegalStateException was not thrown.");
161-
}
162-
catch (IllegalStateException e) {
163-
assertThat(e.getMessage()).contains("Missing variable names are: [type]");
164-
}
153+
assertThatIllegalStateException().isThrownBy(promptTemplate::render)
154+
.withMessageContaining("Missing variable names are: [type]");
165155
}
166156

167157
@Test

vector-stores/spring-ai-neo4j-store/src/test/java/org/springframework/ai/vectorstore/neo4j/Neo4jVectorStoreIT.java

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
import java.util.function.Consumer;
2525
import java.util.stream.Collectors;
2626

27-
import org.junit.Assert;
2827
import org.junit.jupiter.api.BeforeEach;
2928
import org.junit.jupiter.api.Test;
3029
import org.junit.jupiter.api.condition.EnabledIfEnvironmentVariable;
@@ -53,6 +52,7 @@
5352
import org.springframework.context.annotation.Primary;
5453

5554
import static org.assertj.core.api.Assertions.assertThat;
55+
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
5656

5757
/**
5858
* @author Gerrit Meier
@@ -207,14 +207,10 @@ void searchWithFilters() {
207207
assertThat(results).hasSize(1);
208208
assertThat(results.get(0).getId()).isEqualTo(bgDocument.getId());
209209

210-
try {
211-
vectorStore
212-
.similaritySearch(SearchRequest.from(searchRequest).filterExpression("country == NL").build());
213-
Assert.fail("Invalid filter expression should have been cached!");
214-
}
215-
catch (FilterExpressionTextParser.FilterExpressionParseException e) {
216-
assertThat(e.getMessage()).contains("Line: 1:17, Error: no viable alternative at input 'NL'");
217-
}
210+
assertThatExceptionOfType(FilterExpressionTextParser.FilterExpressionParseException.class)
211+
.isThrownBy(() -> vectorStore
212+
.similaritySearch(SearchRequest.from(searchRequest).filterExpression("country == NL").build()))
213+
.withMessageContaining("Line: 1:17, Error: no viable alternative at input 'NL'");
218214
});
219215
}
220216

0 commit comments

Comments
 (0)