application test : mockito
This commit is contained in:
@@ -21,6 +21,22 @@
|
|||||||
<groupId>org.springframework.boot</groupId>
|
<groupId>org.springframework.boot</groupId>
|
||||||
<artifactId>spring-boot-starter</artifactId>
|
<artifactId>spring-boot-starter</artifactId>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-web</artifactId>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-data-jpa</artifactId>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.h2database</groupId>
|
||||||
|
<artifactId>h2</artifactId>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.projectlombok</groupId>
|
||||||
|
<artifactId>lombok</artifactId>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.springframework.boot</groupId>
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
|||||||
@@ -1,46 +0,0 @@
|
|||||||
package com.example.apptest;
|
|
||||||
|
|
||||||
public class Study {
|
|
||||||
|
|
||||||
private StudyStatus status;
|
|
||||||
|
|
||||||
private int limit;
|
|
||||||
|
|
||||||
private String name;
|
|
||||||
|
|
||||||
public Study(int limit, String name) {
|
|
||||||
this.limit = limit;
|
|
||||||
this.name = name;
|
|
||||||
}
|
|
||||||
|
|
||||||
public Study() {
|
|
||||||
}
|
|
||||||
|
|
||||||
public Study(int limit) {
|
|
||||||
if (limit <= 0) {
|
|
||||||
throw new IllegalArgumentException("limit은 0보다 커야 한다.");
|
|
||||||
}
|
|
||||||
this.limit = limit;
|
|
||||||
}
|
|
||||||
|
|
||||||
public StudyStatus getStatus() {
|
|
||||||
return this.status;
|
|
||||||
}
|
|
||||||
|
|
||||||
public int getLimit() {
|
|
||||||
return limit;
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getName() {
|
|
||||||
return name;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public String toString() {
|
|
||||||
return "Study{" +
|
|
||||||
"status=" + status +
|
|
||||||
", limit=" + limit +
|
|
||||||
", name='" + name + '\'' +
|
|
||||||
'}';
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,5 +0,0 @@
|
|||||||
package com.example.apptest;
|
|
||||||
|
|
||||||
public enum StudyStatus {
|
|
||||||
DRAFT, STARTED, ENDED
|
|
||||||
}
|
|
||||||
@@ -0,0 +1,21 @@
|
|||||||
|
package com.example.apptest.domain;
|
||||||
|
|
||||||
|
import lombok.AllArgsConstructor;
|
||||||
|
import lombok.Data;
|
||||||
|
import lombok.NoArgsConstructor;
|
||||||
|
|
||||||
|
import javax.persistence.Entity;
|
||||||
|
import javax.persistence.GeneratedValue;
|
||||||
|
import javax.persistence.Id;
|
||||||
|
|
||||||
|
@Entity
|
||||||
|
@Data
|
||||||
|
@AllArgsConstructor
|
||||||
|
@NoArgsConstructor
|
||||||
|
public class Member {
|
||||||
|
|
||||||
|
@Id @GeneratedValue
|
||||||
|
private Long id;
|
||||||
|
|
||||||
|
private String email;
|
||||||
|
}
|
||||||
@@ -0,0 +1,47 @@
|
|||||||
|
package com.example.apptest.domain;
|
||||||
|
|
||||||
|
import lombok.AllArgsConstructor;
|
||||||
|
import lombok.Data;
|
||||||
|
import lombok.NoArgsConstructor;
|
||||||
|
|
||||||
|
import javax.persistence.Entity;
|
||||||
|
import javax.persistence.GeneratedValue;
|
||||||
|
import javax.persistence.Id;
|
||||||
|
import java.time.LocalDateTime;
|
||||||
|
|
||||||
|
@Entity
|
||||||
|
@Data
|
||||||
|
@AllArgsConstructor
|
||||||
|
@NoArgsConstructor
|
||||||
|
public class Study {
|
||||||
|
|
||||||
|
@Id @GeneratedValue
|
||||||
|
private Long id;
|
||||||
|
|
||||||
|
private StudyStatus status = StudyStatus.DRAFT;
|
||||||
|
|
||||||
|
private int limit;
|
||||||
|
|
||||||
|
private String name;
|
||||||
|
|
||||||
|
private LocalDateTime openedDateTime;
|
||||||
|
|
||||||
|
private Long ownerId;
|
||||||
|
|
||||||
|
public Study(int limit, String name) {
|
||||||
|
this.limit = limit;
|
||||||
|
this.name = name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Study(int limit) {
|
||||||
|
if (limit <= 0) {
|
||||||
|
throw new IllegalArgumentException("limit은 0보다 커야 한다.");
|
||||||
|
}
|
||||||
|
this.limit = limit;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void open() {
|
||||||
|
this.openedDateTime = LocalDateTime.now();
|
||||||
|
this.status = StudyStatus.OPENED;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,5 @@
|
|||||||
|
package com.example.apptest.domain;
|
||||||
|
|
||||||
|
public enum StudyStatus {
|
||||||
|
DRAFT, STARTED, ENDED, OPENED
|
||||||
|
}
|
||||||
@@ -0,0 +1,10 @@
|
|||||||
|
package com.example.apptest.member;
|
||||||
|
|
||||||
|
import com.example.apptest.domain.Member;
|
||||||
|
|
||||||
|
import java.util.Optional;
|
||||||
|
|
||||||
|
public interface MemberService {
|
||||||
|
|
||||||
|
Optional<Member> findById(Long memberId);
|
||||||
|
}
|
||||||
@@ -0,0 +1,9 @@
|
|||||||
|
package com.example.apptest.study;
|
||||||
|
|
||||||
|
import com.example.apptest.domain.Study;
|
||||||
|
import org.springframework.data.jpa.repository.JpaRepository;
|
||||||
|
import org.springframework.stereotype.Repository;
|
||||||
|
|
||||||
|
@Repository
|
||||||
|
public interface StudyRepository extends JpaRepository<Study, Long> {
|
||||||
|
}
|
||||||
@@ -0,0 +1,25 @@
|
|||||||
|
package com.example.apptest.study;
|
||||||
|
|
||||||
|
import com.example.apptest.domain.Member;
|
||||||
|
import com.example.apptest.domain.Study;
|
||||||
|
import com.example.apptest.member.MemberService;
|
||||||
|
|
||||||
|
public class StudyService {
|
||||||
|
|
||||||
|
private final MemberService memberService;
|
||||||
|
private final StudyRepository studyRepository;
|
||||||
|
|
||||||
|
public StudyService(MemberService memberService, StudyRepository studyRepository) {
|
||||||
|
assert memberService != null;
|
||||||
|
assert studyRepository != null;
|
||||||
|
this.memberService = memberService;
|
||||||
|
this.studyRepository = studyRepository;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Study createNewStudy(Long memberId, Study study) {
|
||||||
|
Member member = memberService.findById(memberId)
|
||||||
|
.orElseThrow(() -> new IllegalArgumentException("Member doesn't exist for id: " + memberId));
|
||||||
|
study.setOwnerId(memberId);
|
||||||
|
return studyRepository.save(study);
|
||||||
|
}
|
||||||
|
}
|
||||||
21
application-test/app-test/src/main/resources/application.yml
Normal file
21
application-test/app-test/src/main/resources/application.yml
Normal file
@@ -0,0 +1,21 @@
|
|||||||
|
spring:
|
||||||
|
h2:
|
||||||
|
console:
|
||||||
|
enabled: true
|
||||||
|
path: /h2-console
|
||||||
|
|
||||||
|
datasource:
|
||||||
|
url: jdbc:h2:mem:testDb
|
||||||
|
driver-class-name: org.h2.Driver
|
||||||
|
username: sa
|
||||||
|
password:
|
||||||
|
|
||||||
|
jpa:
|
||||||
|
hibernate:
|
||||||
|
ddl-auto: create
|
||||||
|
generate-ddl: true
|
||||||
|
|
||||||
|
properties:
|
||||||
|
format_sql: true
|
||||||
|
hibernate:
|
||||||
|
show-sql: true
|
||||||
@@ -0,0 +1,19 @@
|
|||||||
|
package com.example.apptest.study;
|
||||||
|
|
||||||
|
import com.example.apptest.member.MemberService;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
import org.mockito.Mockito;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.*;
|
||||||
|
|
||||||
|
class StudyServiceTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void createStudyService() {
|
||||||
|
|
||||||
|
MemberService memberService = Mockito.mock(MemberService.class);
|
||||||
|
StudyRepository studyRepository = Mockito.mock(StudyRepository.class);
|
||||||
|
StudyService studyService = new StudyService(memberService, studyRepository);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user