사랑해 마니마니

Microservice @spring boot 본문

spring boot in action

Microservice @spring boot

분리불안증후군 2018. 4. 21. 15:14

스프링 부트 기본 시작해 보기(microservice v1)

Learn Microservices with Spring Boot (Appress) 따라서 코딩하기

https://github.com/microservices-practical

도메인 만들기(Multiplication.class)


package microservices.book.multiplication.domain;

public class Multiplication {

    private int factorA;
    private int factorB;
    private int result;

    public Multiplication(int factorA, int factorB) {
        this.factorA = factorA;
        this.factorB = factorB;
        this.result = factorA * factorB;
    }

    public int getFactorA() {
        return factorA;
    }

    public int getFactorB() {
        return factorB;
    }

    public int getResult() {
        return result;
    }

    @Override
    public String toString() {
        return "Multiplication{" +
                "factorA=" + factorA +
                ", factorB=" + factorB +
                ", result=" + result +
                '}';
    }
}

서비스 만들기 (Multiplication Service)

package microservices.book.multiplication.service;

import microservices.book.multiplication.domain.Multiplication;

public interface MultiplicationService {
    Multiplication createRandomMultiplication();
}

Dependency 가 있는 외부 서비스의 Interface 만들기 (RandomGenerator Service)

  • Multiplication 서비스가 의존하고 있는 외부 서비스에 대한 Spec(Interface)
package microservices.book.multiplication.service;

public interface RandomGeneratorService {
    int generateRandomFactor();
}

이제 만들었으니 테스트를 해보자 (MultiplicationService Test)

package microservices.book.multiplication.service;

import microservices.book.multiplication.domain.Multiplication;
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 static org.assertj.core.api.Assertions.*;
import static org.mockito.BDDMockito.given;

@RunWith(SpringRunner.class)
@SpringBootTest
public class MultiplicationServiceTest {

    @MockBean
    private RandomGeneratorService randomGeneratorService;

    @Autowired
    private MultiplicationService multiplicationService;

    @Test
    public void createRandomMultiplicationTest() {
        given(randomGeneratorService.generateRandomFactor()).willReturn(50, 30);

        Multiplication multiplication = multiplicationService.createRandomMultiplication();

        assertThat(multiplication.getFactorA()).isEqualTo(50);
        assertThat(multiplication.getFactorB()).isEqualTo(30);

        assertThat(multiplication.getResult()).isEqualTo(1500);

    }

}

RandomGeneratorService는 @MockBean을 이용하여 처음 call이되면 50을 두번째 call이 되면 30을 return하게 함

@MockBean 대상이 되는 RandomGeneratorService는 외부 Dependency가 걸려 있는 녀석으로 세부 구현을 잘 모르는 경우에 사용함. e.g., 타 조직에서 개발하기로 했는 데 아직 개발이 안되어 있지만 어떤 spec으로 개발하겠다고 합의는 되어 있는 경우(여기서는 int 타입의 숫자를 무작위로 생성해 주기로 함)

테스트 실행해 보면 실패(@Autowired하기로 되어 있는 MultiplicationService의 구현 Class가 없어서

MultiplicationService의 구현 Class 만들기

package microservices.book.multiplication.service;

import microservices.book.multiplication.domain.Multiplication;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class MultiplicationServiceImpl implements MultiplicationService {
    private RandomGeneratorService randomGeneratorService;

    @Autowired
    public MultiplicationServiceImpl(RandomGeneratorService randomGeneratorService) {
        this.randomGeneratorService = randomGeneratorService;
    }

    @Override
    public Multiplication createRandomMultiplication() {
        int factorA = randomGeneratorService.generateRandomFactor();
        int factorB = randomGeneratorService.generateRandomFactor();

        return new Multiplication(factorA, factorB);
    }
}

다시 테스트 실행해 보기

테스트 성공 !!!

Comments