react-springboot : api server test (junit5)
This commit is contained in:
@@ -0,0 +1,19 @@
|
||||
package com.example.book.domain;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase;
|
||||
import org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase.Replace;
|
||||
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
// 단위테스트 ( DB 관련 Bean 만 IoC에 등록되면 됨 )
|
||||
|
||||
@Transactional
|
||||
@AutoConfigureTestDatabase(replace = Replace.ANY) // Replace.ANY 가짜 DB로 테스트, Replace.NONE 실제 DB로 테스트
|
||||
@DataJpaTest // Repository 들을 spring container 에 등록해줌
|
||||
class BookRepositoryUnitTest {
|
||||
|
||||
@Autowired
|
||||
private BookRepository bookRepository;
|
||||
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
package com.example.book.service;
|
||||
|
||||
import com.example.book.domain.BookRepository;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
import org.mockito.InjectMocks;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.junit.jupiter.MockitoExtension;
|
||||
|
||||
/**
|
||||
* 단위테스트 ( Service 관련 Bean 만 메모리에 띄움 )
|
||||
* BookService 는 BookRepository 에 의존적이다.
|
||||
* 단위테스트를 위해 BookRepository 를 가짜 객체로 만들어 테스트 ( Mockito )
|
||||
*/
|
||||
|
||||
@ExtendWith(MockitoExtension.class)
|
||||
class BookServiceUnitTest {
|
||||
|
||||
@InjectMocks // BookService 객체가 만들어질 때 BookServiceUnitTest 파일에 @Mock 으로 등록된 객체를 DI
|
||||
private BookService bookService;
|
||||
|
||||
@Mock
|
||||
private BookRepository bookRepository;
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
package com.example.book.web;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
|
||||
import org.springframework.test.web.servlet.MockMvc;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
/**
|
||||
* 통합테스트 ( 모든 Bean 들을 IoC에 올리고 테스트 하는 것 )
|
||||
* WebEnvironment.MOCK = 실제 톰캣이 아닌 다른 톰캣으로 테스트
|
||||
* WebEnvironment.RANDOM_PORT = 실제 톰캣으로 테스트
|
||||
* @AutoConfigureMockMvc = MockMvc 를 IoC에 등록
|
||||
* @Transactional = 각 테스트 함수가 종료되면 트랜잭션을 rollback
|
||||
*
|
||||
*/
|
||||
|
||||
@Transactional
|
||||
@AutoConfigureMockMvc
|
||||
@SpringBootTest(webEnvironment = WebEnvironment.MOCK)
|
||||
public class BookApplicationIntegrationTests {
|
||||
|
||||
@Autowired
|
||||
private MockMvc mockMvc;
|
||||
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
package com.example.book.web;
|
||||
|
||||
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
|
||||
|
||||
// 단위테스트 ( Controller 관련 로직만 테스트 - Filter, ControllerAdvice , ... )
|
||||
|
||||
@WebMvcTest
|
||||
class BookControllerUnitTest {
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user