Skip to content

Commit 5ba5db0

Browse files
committed
GH-159 - Unit tests for JDBC schema resolution.
1 parent 0e2e404 commit 5ba5db0

File tree

3 files changed

+69
-1
lines changed

3 files changed

+69
-1
lines changed

spring-modulith-events/spring-modulith-events-jdbc/pom.xml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,18 @@
5959
<artifactId>spring-boot-starter-jdbc</artifactId>
6060
<scope>test</scope>
6161
</dependency>
62+
63+
<dependency>
64+
<groupId>org.springframework</groupId>
65+
<artifactId>spring-web</artifactId>
66+
<scope>test</scope>
67+
</dependency>
68+
69+
<dependency>
70+
<groupId>jakarta.servlet</groupId>
71+
<artifactId>jakarta.servlet-api</artifactId>
72+
<scope>test</scope>
73+
</dependency>
6274

6375
<!-- Testcontainers -->
6476

spring-modulith-events/spring-modulith-events-jdbc/src/main/java/org/springframework/modulith/events/jdbc/DatabaseSchemaInitializer.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ public DatabaseSchemaInitializer(JdbcOperations jdbcOperations, ResourceLoader r
6262
public void afterPropertiesSet() {
6363

6464
var schemaResourceFilename = databaseType.getSchemaResourceFilename();
65-
var schemaDdlResource = resourceLoader.getResource("classpath:" + schemaResourceFilename);
65+
var schemaDdlResource = resourceLoader.getResource(ResourceLoader.CLASSPATH_URL_PREFIX + schemaResourceFilename);
6666
var schemaDdl = asString(schemaDdlResource);
6767

6868
jdbcOperations.execute(schemaDdl);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/*
2+
* Copyright 2023 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.modulith.events.jdbc;
17+
18+
import static org.assertj.core.api.Assertions.*;
19+
import static org.mockito.ArgumentMatchers.*;
20+
import static org.mockito.Mockito.*;
21+
22+
import org.junit.jupiter.api.Test;
23+
import org.junit.jupiter.api.extension.ExtendWith;
24+
import org.mockito.ArgumentCaptor;
25+
import org.mockito.Mock;
26+
import org.mockito.junit.jupiter.MockitoExtension;
27+
import org.springframework.core.io.ClassPathResource;
28+
import org.springframework.core.io.ResourceLoader;
29+
import org.springframework.jdbc.core.JdbcOperations;
30+
31+
/**
32+
* Unit tests for {@link DatabaseSchemaInitializer}.
33+
*
34+
* @author Oliver Drotbohm
35+
*/
36+
@ExtendWith(MockitoExtension.class)
37+
class DatabaseSchemaInitializerUnitTests {
38+
39+
@Mock JdbcOperations jdbcOperations;
40+
@Mock ResourceLoader resourceLoader;
41+
42+
@Test // GH-159
43+
void loadsSchemaFilesFromClasspath() {
44+
45+
when(resourceLoader.getResource(any())).thenAnswer(it -> {
46+
return new ClassPathResource(it.<String> getArgument(0).substring(ResourceLoader.CLASSPATH_URL_PREFIX.length()));
47+
});
48+
49+
var initializer = new DatabaseSchemaInitializer(jdbcOperations, resourceLoader, DatabaseType.H2);
50+
var captor = ArgumentCaptor.forClass(String.class);
51+
52+
assertThatNoException().isThrownBy(initializer::afterPropertiesSet);
53+
verify(resourceLoader).getResource(captor.capture());
54+
assertThat(captor.getValue()).startsWith(ResourceLoader.CLASSPATH_URL_PREFIX);
55+
}
56+
}

0 commit comments

Comments
 (0)