회원님이 찾고있는 사람이 있었다,하지만 난 의존성 삽입 (Dependency Injection)을 사용하는 것이 꽤 솔루션입니다. 나는 새로운 "중간 사람"클래스를 소개하는 것, BackgroundRunner, 그냥 테스트를 용이하게하기 :
public interface InvokableInBackground {
public void invokeMeInBackground();
}
public class ToBeMocked implements InvokableInBackground {
public void invokeMeInBackground() {
// do stuff
}
}
public class BackgroundRunner {
public void invokeInBackground(InvokableInBackground invokableInBackground) {
new Thread(new Runnable() {
@Override
public void run() {
invokableInBackground.invokeMeInBackground();
}
}).start();
}
}
public class SUT {
final BackgroundRunner backgroundRunner;
final ToBeMocked toBeMocked;
SUT(BackgroundRunner backgroundRunner, ToBeMocked toBeMocked) {
this.backgroundRunner = backgroundRunner;
this.toBeMocked = toBeMocked;
}
void methodToBeTested(...) {
ToBeMocked toBeMocked = ...;
backgroundRunner.invokeInBackground(toBeMocked);
}
}
다음 BackgroundRunner 두 가지 테스트 복식의 ToBeMocked 객체 하나 하나를 제공 할 수 귀하의 단위 테스트 목적. 그런 다음 BackgroundRunner 객체의 invokeInBackground() 메서드가 toBeMocked를 인수로 사용하여 호출되었는지 확인할 수 있습니다. 내가 일하는 가게에서
는 모든 사람이 (그
완벽한 구현, 위 참조) BackgroundRunner 클래스는 테스트를 필요로하지 않는다 충분히 간단
있다는 점에 동의합니다.
군더더기 : 난 그냥 발명 한 BackgroundRunner 클래스는 ExecutorService에의 단순한 버전처럼 엄청 많이 보이는.
단순한 'ExecutorService'처럼 보입니다. 사실, BackgroundRunner [implement'Executor'] (http://docs.oracle.com/javase/7/docs/api/java/util/concurrent /Executor.html),'execute (Runnable)'을 정의하는 인터페이스이다. –