Skip to content

Commit 54c51d1

Browse files
committed
#BAEL-9291: add test configuration and test class
1 parent ab63e1a commit 54c51d1

File tree

2 files changed

+70
-0
lines changed

2 files changed

+70
-0
lines changed
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package com.baeldung.springai.docker.modelrunner;
2+
3+
import static org.assertj.core.api.AssertionsForInterfaceTypes.assertThat;
4+
5+
import org.junit.jupiter.api.BeforeEach;
6+
import org.junit.jupiter.api.Test;
7+
import org.springframework.beans.factory.annotation.Autowired;
8+
import org.springframework.boot.test.context.SpringBootTest;
9+
import org.springframework.boot.test.web.client.TestRestTemplate;
10+
import org.springframework.boot.test.web.server.LocalServerPort;
11+
import org.springframework.context.annotation.Import;
12+
import org.springframework.http.ResponseEntity;
13+
14+
15+
@Import(TestcontainersConfiguration.class)
16+
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
17+
class ModelRunnerApplicationTest {
18+
19+
@LocalServerPort
20+
private int port;
21+
22+
@Autowired
23+
private TestRestTemplate restTemplate;
24+
25+
private String baseUrl;
26+
27+
@BeforeEach
28+
void setUp() {
29+
baseUrl = "http://localhost:" + port;
30+
}
31+
32+
@Test
33+
void givenMessage_whenCallChatController_thenSuccess() {
34+
// given
35+
String userMessage = "Hello, how are you?";
36+
37+
// when
38+
ResponseEntity<String> response = restTemplate.getForEntity(
39+
baseUrl + "/chat?message=" + userMessage, String.class);
40+
41+
// then
42+
assertThat(response.getStatusCode().is2xxSuccessful()).isTrue();
43+
assertThat(response.getBody()).isNotEmpty();
44+
}
45+
46+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.baeldung.springai.docker.modelrunner;
2+
3+
import org.springframework.boot.test.context.TestConfiguration;
4+
import org.springframework.context.annotation.Bean;
5+
import org.springframework.test.context.DynamicPropertyRegistrar;
6+
import org.testcontainers.containers.DockerModelRunnerContainer;
7+
8+
@TestConfiguration(proxyBeanMethods = false)
9+
class TestcontainersConfiguration {
10+
11+
@Bean
12+
DockerModelRunnerContainer socat() {
13+
return new DockerModelRunnerContainer("alpine/socat:1.8.0.1");
14+
}
15+
16+
@Bean
17+
DynamicPropertyRegistrar properties(DockerModelRunnerContainer dmr) {
18+
return (registrar) -> {
19+
registrar.add("spring.ai.openai.base-url", dmr::getOpenAIEndpoint);
20+
registrar.add("spring.ai.openai.api-key", () -> "test-api-key");
21+
registrar.add("spring.ai.openai.chat.options.model", () -> "ai/gemma3");
22+
};
23+
}
24+
}

0 commit comments

Comments
 (0)