3Tier 로 고치기
3Tier 로 고치기
RandomGeneratorService 테스트 해보기
RandomGeneratorService는 11~100까지의 난수를 생성시키는 서비스임
이 서비스 Spec을 만족하는지 체크해 보기
package microservices.book.multiplication.service;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.test.context.junit4.SpringRunner;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import static org.assertj.core.api.Assertions.*;
import static org.mockito.BDDMockito.given;
@RunWith(SpringRunner.class)
@SpringBootTest
public class RandomGeneratorServiceTest {
@Autowired
private RandomGeneratorService randomGeneratorService;
@Test
public void generateRandomFactorIsBetweenExpectedLimits() throws Exception{
//1000번 generateRandomFactor call을 하고 그 결과를 randomFactors에 저장
List randomFactors =
IntStream.range(0, 1000)
.map(i -> randomGeneratorService.generateRandomFactor())
.boxed()
.collect(Collectors.toList());
assertThat(randomFactors).containsOnlyElementsOf(
IntStream.range(11, 100)
.boxed()
.collect(Collectors.toList())
);
}
}
randomGeneratorService의 구현class가 없어서 fail남... (통합 테스트를 해 보든가? 아니면 구현 class의 테스트 코드를 한번 확인해 보자.)
RandomGeneratorService 구현 Class 테스트 해보기
RandomGeneratorServiceTest를 조금 고쳐보자
public class RandomGeneratorServiceImplTest {
private RandomGeneratorService randomGeneratorService;
@Before
public void setUp() {
randomGeneratorService = new randomGeneratorServiceImpl();
}
또는
public class RandomGeneratorServiceImplTest {
private RandomGeneratorServiceImpl randomGeneratorServiceImpl;
@Before
public void setUp() {
randomGeneratorServiceImpl = new randomGeneratorServiceImpl();
}
이렇게 고치고 실행을 해보면 randomGeneratorServiceImpl 클래스가 없다고 에러남
IDE(Intellij or Eclipse, 난 Intellij) 의 도움을 받아서 randomGeneratorServiceImpl를 생성
RandomGeneratorService 구현 Class 만들기고 Test fail 시키기
Intellij가 자동 생성해 준 코드는
package microservices.book.multiplication.service;
public class randomGeneratorServiceImpl implements RandomGeneratorService {
@Override
public int generateRandomFactor() {
return 0;
}
}
여기에 @Autowired가 될 수 있도록 @Service 어노테이션 추가하기
package microservices.book.multiplication.service;
public class randomGeneratorServiceImpl implements RandomGeneratorService {
@Override
public int generateRandomFactor() {
return 0;
}
}
테스트 Fail남... (빨간 progress bar!)
(CLI - 명령창에서 gradle에서 실행할 때는 gradle -Dtest.single=RandomGeneratorServiceImplTest test)
(책은 메이븐 기준: mvnw test -Dtest=RandomGeneratorServiceImplTest)
RandomGeneratorService 구현 Class 만들기고 Test 성공 시키기
package microservices.book.multiplication.service;
import org.springframework.stereotype.Service;
import java.util.Random;
@Service
public class randomGeneratorServiceImpl implements RandomGeneratorService {
final static int MINIMUM_FACTOR = 11;
final static int MAXIMUM_FACTOR = 99;
@Override
public int generateRandomFactor() {
return new Random().nextInt((MAXIMUM_FACTOR - MINIMUM_FACTOR) + 1 ) + MINIMUM_FACTOR;
}
}
다시 테스트 실행시키면 1 test passed라는 표시로 초록색 progress bar 보여주기
구현이 바꼈으니깐 테스트 코드도 수정하자(
//@RunWith(SpringRunner.class)
//@SpringBootTest
public class MultiplicationServiceTest {
//@MockBean
@Mock
private RandomGeneratorService randomGeneratorService;
//@Autowired
private MultiplicationService multiplicationService;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
multiplicationService = new MultiplicationServiceImpl(randomGeneratorService);
}
mockito 사용법은 http://jdm.kr/blog/222
@RunWith(SpringRunner.class), @SpringBootTest를 안쓰게 되서 훨씬 빠르고 부드러움(!)
Spring boot의 reference project인 https://github.com/spring-io/sagan를 봐도 @SopringBootTest는 잘 안씀(@Autowired 포함해서)