This repository has been archived by the owner on Aug 13, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
#36 junit 이용한 테스트 코드 작성 #37
Open
junshock5
wants to merge
18
commits into
feature/30
Choose a base branch
from
feature/36
base: feature/30
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
daeb192
#36 테스트 코드 작성
junshock5 cb4047e
#36 테스트 코드 작성
junshock5 73d86e6
#36 테스트 코드 작성
junshock5 fb97fc5
#36 테스트 코드 작성
junshock5 df31bea
#36 테스트 코드 작성
junshock5 cffd7d9
ProductServiceImplTest 의 updateProducts 메서드에 mapper, servie 단언 추가
junshock5 28705c7
ProductServiceImplTest 불필요한 임시변수 삭제, text->test 변경
junshock5 bb544f6
#36 junit 이용한 테스트 코드 작성
junshock5 35d6dbf
#36 junit 이용한 테스트 코드 작성
junshock5 50d2dc0
스레드 로컬에 final 키워드 추가
junshock5 74231d2
release mysql, redis set config
junshock5 8025b04
profile update release
junshock5 b9770f3
server port=80
junshock5 cbf161f
add debug = true
junshock5 bf085b0
debug=true
junshock5 98e935e
port=9000
junshock5 df3561b
spring-boot-maven-plugin version delete
junshock5 566ca12
port 3306 update
junshock5 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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
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
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
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
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 |
---|---|---|
@@ -1,8 +1,8 @@ | ||
# Server | ||
server.port= | ||
server.port=8080 | ||
|
||
# profile name = local, dev, prod | ||
spring.profiles.active=dev | ||
spring.profiles.active=release | ||
|
||
# session | ||
spring.session.store-type=redis |
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
64 changes: 64 additions & 0 deletions
64
src/test/java/com/market/server/service/Impl/CategoryServiceImplTest.java
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,64 @@ | ||
package com.market.server.service.Impl; | ||
|
||
import com.market.server.dao.ProductDao; | ||
import com.market.server.dto.CategoryDTO; | ||
import com.market.server.dto.ProductDTO; | ||
import com.market.server.mapper.CategoryMapper; | ||
import com.market.server.mapper.ProductMapper; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.runner.RunWith; | ||
import org.mockito.InjectMocks; | ||
import org.mockito.Mock; | ||
import org.mockito.MockitoAnnotations; | ||
import org.mockito.junit.MockitoJUnitRunner; | ||
|
||
import java.util.Date; | ||
import java.util.List; | ||
|
||
import static org.junit.jupiter.api.Assertions.*; | ||
import static org.mockito.BDDMockito.given; | ||
|
||
@RunWith(MockitoJUnitRunner.class) | ||
class CategoryServiceImplTest { | ||
|
||
@InjectMocks | ||
CategoryServiceImpl categoryService; | ||
|
||
@Mock | ||
CategoryMapper categoryMapper; | ||
|
||
// 새로운 카테고리 객체를 생성하여 반환한다. | ||
public CategoryDTO generateCategory() { | ||
MockitoAnnotations.initMocks(this); // mock all the field having @Mock annotation | ||
CategoryDTO categoryDTO = CategoryDTO.builder() | ||
.id(1) | ||
.name("testName") | ||
.sortStatus(CategoryDTO.SortStatus.NEWEST) | ||
.searchCount(1000) | ||
.pagingStartOffset(0) | ||
.build(); | ||
return categoryDTO; | ||
} | ||
|
||
@Test | ||
void register() { | ||
CategoryDTO categoryDTO = generateCategory(); | ||
given(categoryMapper.register(categoryDTO)).willReturn(categoryDTO.getId()); | ||
categoryMapper.register(categoryDTO); | ||
} | ||
|
||
@Test | ||
void update() { | ||
CategoryDTO categoryDTO = generateCategory(); | ||
categoryDTO.setName("testName2"); | ||
categoryMapper.updateCategory(categoryDTO); | ||
assertEquals(categoryDTO.getName(),("testName2")); | ||
} | ||
|
||
@Test | ||
void delete() { | ||
CategoryDTO categoryDTO = generateCategory(); | ||
categoryDTO.setId(1); | ||
categoryMapper.deleteCategory(1); | ||
} | ||
} |
93 changes: 93 additions & 0 deletions
93
src/test/java/com/market/server/service/Impl/ProductSearchServiceImplTest.java
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,93 @@ | ||
package com.market.server.service.Impl; | ||
|
||
import com.market.server.dao.ProductDao; | ||
import com.market.server.dto.CategoryDTO; | ||
import com.market.server.dto.ProductDTO; | ||
import com.market.server.dto.UserDTO; | ||
import com.market.server.mapper.ProductMapper; | ||
import com.market.server.utils.SHA256Util; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.runner.RunWith; | ||
import org.mockito.InjectMocks; | ||
import org.mockito.Mock; | ||
import org.mockito.MockitoAnnotations; | ||
import org.mockito.junit.MockitoJUnitRunner; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Date; | ||
import java.util.List; | ||
|
||
import static org.junit.jupiter.api.Assertions.*; | ||
import static org.mockito.BDDMockito.given; | ||
|
||
@RunWith(MockitoJUnitRunner.class) | ||
class ProductSearchServiceImplTest { | ||
|
||
@InjectMocks | ||
ProductServiceImpl productService; | ||
|
||
@InjectMocks | ||
ProductSearchServiceImpl productSearchService; | ||
|
||
@Mock | ||
ProductMapper productMapper; | ||
|
||
@Mock | ||
ProductDao productDao; | ||
|
||
// 새로운 물품 객체 리스트를 생성하여 반환한다. | ||
public List<ProductDTO> generateProductList() { | ||
MockitoAnnotations.initMocks(this); // mock all the field having @Mock annotation | ||
List<ProductDTO> productDTOList = new ArrayList<ProductDTO>(); | ||
for (int i = 0; i < 5; i++) { | ||
ProductDTO productDTO = ProductDTO.builder() | ||
.id(i) | ||
.price(1000) | ||
.accountId(1) | ||
.title("testProductTitle") | ||
.contents("testProductContents") | ||
.status(ProductDTO.Status.NEW) | ||
.istrade(true) | ||
.updatetime(new Date()) | ||
.deliveryprice(3000) | ||
.dibcount(1) | ||
.categoryId(1) | ||
.build(); | ||
productDTOList.add(productDTO); | ||
} | ||
return productDTOList; | ||
} | ||
|
||
public UserDTO generateUser() { | ||
UserDTO userDTO = new UserDTO(); | ||
userDTO.setId("textUserId"); | ||
userDTO.setPassword(SHA256Util.encryptSHA256("testPassword")); | ||
userDTO.setName("testUserName"); | ||
userDTO.setPhone("010-1111-2222"); | ||
userDTO.setAddress("testAdress"); | ||
userDTO.setStatus(UserDTO.Status.DEFAULT); | ||
userDTO.setCreatetime(new Date()); | ||
userDTO.setUpdatetime(new Date()); | ||
userDTO.setAddmin(false); | ||
userDTO.setAccountId(1); | ||
return userDTO; | ||
} | ||
|
||
@Test | ||
void findAllProductsByCacheId() { | ||
MockitoAnnotations.initMocks(this); // mock all the field having @Mock annotation | ||
UserDTO userDTO = generateUser(); | ||
productDao.findAllProductsByCacheId(userDTO.getId()); | ||
} | ||
|
||
@Test | ||
void getProducts() { | ||
List<ProductDTO> productDTOList = generateProductList(); | ||
given(productMapper.selectMyProducts(1)).willReturn(productDTOList); | ||
given(productMapper.selectMyProducts(999)).willReturn(null); | ||
productMapper.selectMyProducts(1); | ||
|
||
UserDTO userDTO = generateUser(); | ||
productService.getMyProducts(userDTO.getAccountId()); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
findAllProductsByCacheId
내부에서 일어나는 로직 검사라던지 리턴값이라던지 그런 스펙에 대한 검사가 없네요~There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
선언 후에
redisTemplate.opsForList().rightPush(ProductDTO.DEFAULT_PRODUCT_SEARCH_CACHE_KEY, productDTO);
여기서 redisTemplate이 mock객체로 null은 아니지만 List,push 할때 null객체로 에러가 납니다.
접근을 어떻게 해야 할까요??