-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
60 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package springbook.chapter07; | ||
|
||
import org.junit.jupiter.api.AfterEach; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.springframework.jdbc.core.JdbcTemplate; | ||
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabase; | ||
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder; | ||
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType; | ||
|
||
import java.util.List; | ||
import java.util.Map; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
public class EmbeddedDbTest { | ||
EmbeddedDatabase db; | ||
JdbcTemplate template; | ||
|
||
@BeforeEach | ||
void setUp() { | ||
db = new EmbeddedDatabaseBuilder() | ||
.setType(EmbeddedDatabaseType.H2) | ||
.addScript("schema.sql") | ||
.addScript("data.sql") | ||
.build(); | ||
|
||
template = new JdbcTemplate(db); | ||
} | ||
|
||
@AfterEach | ||
void tearDown() { | ||
db.shutdown(); | ||
} | ||
|
||
@Test | ||
void initData() { | ||
List<Map<String,Object>> list = template.queryForList("select * from sqlmap"); | ||
assertThat(list).hasSize(2); | ||
|
||
assertThat(list.get(0).get("key_")).isEqualTo("KEY1"); | ||
assertThat(list.get(0).get("sql_")).isEqualTo("SQL1"); | ||
assertThat(list.get(1).get("key_")).isEqualTo("KEY2"); | ||
assertThat(list.get(1).get("sql_")).isEqualTo("SQL2"); | ||
} | ||
|
||
@Test | ||
void insert() { | ||
template.update("insert into sqlmap(key_,sql_) values(?,?)", "KEY3" , "SQL3"); | ||
|
||
assertThat(template.queryForList("select * from sqlmap")).hasSize(3); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
INSERT INTO SQLMAP(KEY_, SQL_) VALUES('KEY1', 'SQL1'); | ||
INSERT INTO SQLMAP(KEY_, SQL_) VALUES('KEY2', 'SQL2'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
CREATE TABLE SQLMAP ( | ||
KEY_ VARCHAR(100) PRIMARY KEY, | ||
SQL_ VARCHAR(1000) NOT NULL | ||
); |