Justin의 개발 로그

Gradle.build 설정

dependencies {
    testCompile('org.junit.jupiter:junit-jupiter-api')
    testRuntimeOnly('org.junit.jupiter:junit-jupiter-engine')
    testImplementation ('org.mockito:mockito-core:2.26.0')
}

 

[테스트 코드]

package mock;

import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.mock;

public class GameGenMockTest {
    @Test
    void mockTest() {
        GameNumGen genMock = mock(GameNumGen.class);
        given(genMock.generate(GameLevel.EASY)).willReturn("123");

        String num = genMock.generate(GameLevel.EASY);
        assertEquals(num, "123");
    }
}

 

package mock;

public enum GameLevel {
    EASY
}

 

package mock;

public interface GameNumGen {
    String generate(GameLevel level);
}

[Mock 으로 Exception 테스트]

    @Test
    void mockThrowTest() {
        GameNumGen genMock = mock(GameNumGen.class);
        given(genMock.generate(null)).willThrow(IllegalArgumentException.class);

        assertThrows(IllegalArgumentException.class,
                ()-> genMock.generate(null));
    }

 

profile

Justin의 개발 로그

@라이프노트

포스팅이 좋았다면 "좋아요❤️" 또는 "구독👍🏻" 해주세요!