May 2025
Beginner to intermediate
424 pages
7h 7m
Korean
이 작품은 AI를 사용하여 번역되었습니다. 여러분의 피드백과 의견을 환영합니다: translation-feedback@oreilly.com
모의 테스트는 객체 지향 코드를 프로덕션 종속성에서 분리하는 일반적인 기술입니다. Kotlin에서 더 나은 솔루션을 사용할 수 있나요?
이 는 16장에 이은 짧은 보너스 챕터입니다. 이 챕터에서는 대부분의 메서드가 사용되지 않았음에도 불구하고 두 개의 다중 메서드 인터페이스를 구현해야 했기 때문에 모의 테스트를 사용했습니다. 리팩터링을 통해 다중 메서드 인터페이스의 종속성을 실제로 작업을 수행하는 데 필요한 두 개의 연산에만 대한 종속성으로 대체했습니다. 그러나 테스트는 여전히 전체 인터페이스에 대한 모의 작업을 한 다음 테스트 대상(Recommendations)에게 필요한 메서드의 참조를 전달하고 있습니다:
publicclassRecommendationsTests{privatefinalDistanceCalculatordistanceCalculator=mock(DistanceCalculator.class);privatefinalFeaturedDestinationsfeaturedDestinations=mock(FeaturedDestinations.class);privatefinalRecommendationsrecommendations=newRecommendations(featuredDestinations::findCloseTo,distanceCalculator::distanceInMetersBetween);...}
이 테스트는 givenFeaturedDestinationsFor 와 givenADistanceBetween 메서드 뒤에 있는 모킹을 추상화합니다:
@Testpublicvoidreturns_recommendations_for_single_location(){givenFeaturedDestinationsFor(paris,List.of(eiffelTower,louvre));givenADistanceBetween(paris,eiffelTower,5000);givenADistanceBetween(paris,louvre,1000);assertEquals(List.of(newFeaturedDestinationSuggestion(paris,louvre,1000),newFeaturedDestinationSuggestion(paris,eiffelTower,5000)),recommendations.recommendationsFor(Set.of(paris)));}