을 가지고 나는 객체 같이 있습니다스칼라 : 내 스칼라 개체를 Moking 외부 의존성
// I want to test this Object
object MyObject {
protected val retryHandler: HttpRequestRetryHandler = new HttpRequestRetryHandler {
def retryRequest(exception: IOException, executionCount: Int, context: HttpContext): Boolean = {
true // implementation
}
}
private val connectionManager: PoolingHttpClientConnectionManager = new PoolingHttpClientConnectionManager
val httpClient: CloseableHttpClient = HttpClients.custom
.setConnectionManager(connectionManager)
.setRetryHandler(retryHandler)
.build
def methodPost = {
//create new context and new Post instance
val post = new HttpPost("url")
val res = httpClient.execute(post, HttpClientContext.create)
// check response code and then take action based on response code
}
def methodPut = {
// same as methodPost except use HttpPut instead HttpPost
}
}
내가 세션 객체와 같은 종속 개체를 조롱하여이 객체를 테스트 할. 이것을 달성하는 방법? 나는 Mokito 또는 더 나은 방법을 사용하여 그것을 할 수 있습니까? 경우 예. 방법? 이 클래스의 디자인이 더 좋습니까?
나는 의존성 주입을 생각했지만 구현 방법을 알 수 없었다. 내가 가진 한 가지 접근법은 각 메소드에 대한 매개 변수로 httpClient를 사용하는 것이 었습니다 (객체 클래스는 Java와 마찬가지로 스칼라에서 생성자를 가질 수 없기 때문에 Put 및 Post는 여전히 새로 작성됩니다). 그러나이 경우 재 시도 및 기타 실패에 대한 제어가 느슨해집니다 케이스 취급. –