Skip to Content
Java에서 Kotlin으로
book

Java에서 Kotlin으로

by Duncan McGregor, Nat Pryce
May 2025
Beginner to intermediate
424 pages
7h 7m
Korean
O'Reilly Media, Inc.
Book available
Content preview from Java에서 Kotlin으로

17장. 맵에 대한 모의

이 작품은 AI를 사용하여 번역되었습니다. 여러분의 피드백과 의견을 환영합니다: translation-feedback@oreilly.com

모의 테스트는 객체 지향 코드를 프로덕션 종속성에서 분리하는 일반적인 기술입니다. Kotlin에서 더 나은 솔루션을 사용할 수 있나요?

이 는 16장에 이은 짧은 보너스 챕터입니다. 이 챕터에서는 대부분의 메서드가 사용되지 않았음에도 불구하고 두 개의 다중 메서드 인터페이스를 구현해야 했기 때문에 모의 테스트를 사용했습니다. 리팩터링을 통해 다중 메서드 인터페이스의 종속성을 실제로 작업을 수행하는 데 필요한 두 개의 연산에만 대한 종속성으로 대체했습니다. 그러나 테스트는 여전히 전체 인터페이스에 대한 모의 작업을 한 다음 테스트 대상(Recommendations)에게 필요한 메서드의 참조를 전달하고 있습니다:

public class RecommendationsTests {

    private final DistanceCalculator distanceCalculator =
        mock(DistanceCalculator.class);
    private final FeaturedDestinations featuredDestinations =
        mock(FeaturedDestinations.class);
    private final Recommendations recommendations = new Recommendations(
        featuredDestinations::findCloseTo,
        distanceCalculator::distanceInMetersBetween
    );
    ...
}

이 테스트는 givenFeaturedDestinationsForgivenADistanceBetween 메서드 뒤에 있는 모킹을 추상화합니다:

@Test
public void returns_recommendations_for_single_location() {
    givenFeaturedDestinationsFor(paris,
        List.of(
            eiffelTower,
            louvre
        ));
    givenADistanceBetween(paris, eiffelTower, 5000);
    givenADistanceBetween(paris, louvre, 1000);

    assertEquals(
        List.of(
            new FeaturedDestinationSuggestion(paris, louvre, 1000),
            new FeaturedDestinationSuggestion(paris, eiffelTower, 5000)
        ),
        recommendations.recommendationsFor(Set.of(paris))
    );
}
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

코드 밖 커뮤니케이션

코드 밖 커뮤니케이션

재퀴 리드
실리콘밸리 리더십

실리콘밸리 리더십

김정혜, 마이클 롭

Publisher Resources

ISBN: 9798341656055Supplemental Content