Skip to content

Commit

Permalink
feat : seperate test database
Browse files Browse the repository at this point in the history
  • Loading branch information
wwan13 committed Jun 24, 2023
1 parent 1b4ba45 commit ea1b34b
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 1 deletion.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ dependencies {
implementation 'org.springframework.boot:spring-boot-starter-hateoas'
implementation 'org.springframework.boot:spring-boot-starter-web'
compileOnly 'org.projectlombok:lombok'
implementation 'com.mysql:mysql-connector-j'
runtimeOnly 'com.mysql:mysql-connector-j'
runtimeOnly 'com.h2database:h2'
annotationProcessor 'org.projectlombok:lombok'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package com.likelionsch.simpleapiexample.item;

import lombok.Builder;
import lombok.Data;

@Data
@Builder
public class ItemDto {

private String name;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package com.likelionsch.simpleapiexample.item;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.jdbc.EmbeddedDatabaseConnection;
import org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.MediaType;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import org.springframework.test.web.servlet.MockMvc;

import static org.junit.jupiter.api.Assertions.*;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;

@SpringBootTest
@ExtendWith(SpringExtension.class)
@AutoConfigureMockMvc
class ItemControllerTest {

@Autowired
MockMvc mockMvc;
@Autowired
ItemService itemService;

@BeforeEach
public void setUp() {
ItemDto itemDto1 = ItemDto.builder()
.name("item1")
.price(3000)
.build();
itemService.createItem(itemDto1);

ItemDto itemDto2 = ItemDto.builder()
.name("item2")
.price(4000)
.build();
itemService.createItem(itemDto2);
}

@Test
public void getAllItems() throws Exception {

mockMvc.perform(get("/api/item"))
.andDo(print());

}

}
14 changes: 14 additions & 0 deletions src/test/resources/application.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
spring:
datasource:
url: jdbc:h2:tcp://localhost/~/simpleexample
username: sa
password:
driver-class-name: org.h2.Driver
jpa:
hibernate:
ddl-auto: create
properties:
hibernate:
format_sql: true
logging.level:
org.hibernate.SQL: debug

0 comments on commit ea1b34b

Please sign in to comment.