테스트코드 작성
This commit is contained in:
@@ -1,7 +1,10 @@
|
||||
<template>
|
||||
<div v-if="!pending">
|
||||
<article v-for="article in articles" v-bind:key="article.id">
|
||||
<router-link :to="{name: 'DetailArticle', params: {id: article.id}}"><span>{{ article.title }}</span></router-link>
|
||||
<router-link :to="{name: 'DetailArticle', params: {id: article.id}}">
|
||||
<span>{{ article.title }}</span>
|
||||
</router-link>
|
||||
<span @click="clickUser">{{ article.user.name }}</span>
|
||||
</article>
|
||||
|
||||
<br>
|
||||
@@ -62,6 +65,9 @@
|
||||
console.log(e);
|
||||
}
|
||||
|
||||
},
|
||||
clickUser(evt) {
|
||||
console.log(evt.target);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -13,6 +13,7 @@ import java.time.LocalDateTime;
|
||||
@Entity
|
||||
@Getter
|
||||
@NamedQuery(name = "findByEmail", query = "select u from User u where u.email = :email")
|
||||
@NamedQuery(name = "findAllUser", query = "select u from User u order by u.id asc")
|
||||
@NoArgsConstructor
|
||||
public class User {
|
||||
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package com.example.vue.domain.user;
|
||||
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.data.domain.Pageable;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
import javax.persistence.EntityManager;
|
||||
@@ -26,4 +27,14 @@ public class UserRepository {
|
||||
return Optional.ofNullable(em.find(User.class, id));
|
||||
}
|
||||
|
||||
public List<User> findAll(Pageable pageable) {
|
||||
int page = pageable.getPageNumber();
|
||||
int size = pageable.getPageSize();
|
||||
|
||||
return em.createNamedQuery("findAllUser", User.class)
|
||||
.setFirstResult(page * size)
|
||||
.setMaxResults(page * size + size)
|
||||
.getResultList();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,8 +1,12 @@
|
||||
package com.example.vue.domain.user;
|
||||
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.data.domain.Pageable;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
|
||||
@Service
|
||||
@RequiredArgsConstructor
|
||||
public class UserService {
|
||||
@@ -12,4 +16,8 @@ public class UserService {
|
||||
public User findById(Long id) {
|
||||
return userRepository.findById(id).orElseThrow(UserException.passNoExistExceptionSupplier(id));
|
||||
}
|
||||
|
||||
public List<User> findAll(Pageable pageable) {
|
||||
return userRepository.findAll(pageable);
|
||||
}
|
||||
}
|
||||
|
||||
25
src/test/java/com/example/vue/BaseServiceTest.java
Normal file
25
src/test/java/com/example/vue/BaseServiceTest.java
Normal file
@@ -0,0 +1,25 @@
|
||||
package com.example.vue;
|
||||
|
||||
import com.example.vue.domain.article.Article;
|
||||
import com.example.vue.domain.article.ArticleRepository;
|
||||
import com.example.vue.domain.user.User;
|
||||
import com.example.vue.domain.user.UserRepository;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.data.domain.PageRequest;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component
|
||||
@RequiredArgsConstructor
|
||||
public class BaseServiceTest {
|
||||
|
||||
private final UserRepository userRepository;
|
||||
private final ArticleRepository articleRepository;
|
||||
|
||||
public User findTestUser() {
|
||||
return userRepository.findAll(PageRequest.of(0, 1)).get(0);
|
||||
}
|
||||
|
||||
public Article findTestArticle() {
|
||||
return articleRepository.findAll(PageRequest.of(0, 1)).get(0);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
package com.example.vue.article;
|
||||
|
||||
public class ArticleServiceException {
|
||||
}
|
||||
@@ -0,0 +1,62 @@
|
||||
package com.example.vue.article;
|
||||
|
||||
import com.example.vue.BaseServiceTest;
|
||||
import com.example.vue.domain.article.Article;
|
||||
import com.example.vue.domain.article.ArticleRequestDto;
|
||||
import com.example.vue.domain.article.ArticleService;
|
||||
import com.example.vue.domain.user.User;
|
||||
import com.example.vue.domain.user.UserService;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.data.domain.PageRequest;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
@SpringBootTest
|
||||
public class ArticleServiceTest {
|
||||
|
||||
@Autowired
|
||||
private BaseServiceTest baseTest;
|
||||
|
||||
@Autowired
|
||||
private ArticleService articleService;
|
||||
|
||||
@Autowired
|
||||
private UserService userService;
|
||||
|
||||
@Test
|
||||
@Transactional
|
||||
public void postArticle() {
|
||||
ArticleRequestDto articleRequestDto = new ArticleRequestDto();
|
||||
articleRequestDto.setTitle("title");
|
||||
articleRequestDto.setTitle("content");
|
||||
User user = baseTest.findTestUser();
|
||||
|
||||
articleService.save(articleRequestDto, user);
|
||||
}
|
||||
|
||||
@Test
|
||||
@Transactional
|
||||
public void putArticle() {
|
||||
Article article = baseTest.findTestArticle();
|
||||
ArticleRequestDto articleRequestDto = new ArticleRequestDto();
|
||||
articleRequestDto.setTitle("update title");
|
||||
articleRequestDto.setTitle("update content");
|
||||
|
||||
articleService.update(article.getId(), articleRequestDto, baseTest.findTestUser());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getArticle() {
|
||||
User user = baseTest.findTestUser();
|
||||
|
||||
Long articleId = baseTest.findTestArticle().getId();
|
||||
articleService.findById(articleId, user);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getArticles() {
|
||||
articleService.findAll(PageRequest.of(0, 10), baseTest.findTestUser());
|
||||
}
|
||||
|
||||
}
|
||||
12
src/test/java/com/example/vue/user/UserServiceException.java
Normal file
12
src/test/java/com/example/vue/user/UserServiceException.java
Normal file
@@ -0,0 +1,12 @@
|
||||
package com.example.vue.user;
|
||||
|
||||
import com.example.vue.domain.article.ArticleException;
|
||||
|
||||
public class UserServiceException {
|
||||
|
||||
public static class NoExist extends RuntimeException {
|
||||
public NoExist() {
|
||||
super("사용자가 존재하지 않습니다.");
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user