2010-05-11 2 views
9

파일이 변경된 경우 매 2 초마다 검사하는 매우 간단한 파일 감시자 클래스가 있습니다. 그렇다면 onChange 메서드 (void)가 호출됩니다. 단위 테스트에서 onChange 메서드가 호출되는지 쉽게 확인 할 수 있습니까?JUnit : void 메서드가 호출되는지 확인

코드 :

public class PropertyFileWatcher extends TimerTask { 
    private long timeStamp; 
    private File file; 

    public PropertyFileWatcher(File file) { 
     this.file = file; 
     this.timeStamp = file.lastModified(); 
    } 

    public final void run() { 
     long timeStamp = file.lastModified(); 

     if (this.timeStamp != timeStamp) { 
      this.timeStamp = timeStamp; 
      onChange(file); 
     } 
    } 

    protected void onChange(File file) { 
     System.out.println("Property file has changed"); 
    } 
} 

테스트 :

@Test 
public void testPropertyFileWatcher() throws Exception { 
    File file = new File("testfile"); 
    file.createNewFile(); 
    PropertyFileWatcher propertyFileWatcher = new PropertyFileWatcher(file); 

    Timer timer = new Timer(); 
    timer.schedule(propertyFileWatcher, 2000); 

    FileWriter fw = new FileWriter(file); 
    fw.write("blah"); 
    fw.close(); 

    Thread.sleep(8000); 
    // check if propertyFileWatcher.onChange was called 

    file.delete(); 
} 

답변

16

Mockito으로, 당신이 방법은 한 번 이상/결코 호출되는지 여부를 확인할 수 있습니다.

this page

예에 점 4를 참조하십시오

내가 알고있는 것처럼
verify(mockedObject, times(1)).onChange(); // times(1) is the default and can be omitted 
+0

mockito뿐 아니라 mocking 프레임 워크를 사용할 수 있습니다. EasyMock 또는 jMock을보고 좋아하는 것을 고르십시오. 단위 테스트를 작성하는 경험 법칙은 제어 할 수있는 객체 만 조롱해야한다는 것입니다. 다시 말해 mock 객체는 테스트중인 메소드에 대한 생성자 인수/설정자 또는 매개 변수를 사용하여 테스트중인 클래스에서 사용할 수있게해야합니다. 이 논리를 사용하면 테스트중인 클래스의 메서드 내에서 만들어진 정적 호출, 최종 또는 개인 또는 "새"개체를 모의 할 수 없습니다. – Kartik

+0

EasyMock에서 어떻게 할 수 있습니까? 나는이 문서가 부족하다는 것을 알게된다. 다음과 같이 PropertyFileWatcher 모의 객체를 만들면 PropertyFileWatcher propertyFileWatcher = createMockBuilder (PropertyFileWatcher.class) .withConstructor (file) .createMock(); onChange에 예상 호출을 기록하고 다시 재생합니다. propertyFileWatcher.onChange (file); 재생 (propertyFileWatcher); onChnage 메소드가 즉시 호출되고 정보가 sysout에 인쇄되지만, 메소드가 호출되었는지 여부를 확인하고 싶습니다. – nkr1pt

+0

mockito를 사용하여 AtomicBoolean 솔루션을 수행 할 수 없습니다. –

4

, 당신의 PropertyFileWatcher가 서브 클래스 화하기위한 것입니다. 그래서 다음과 같이 서브 클래스를 만들지 마십시오 :

class TestPropertyFileWatcher extends PropertyFileWatcher 
{ 
    boolean called = false; 
    protected void onChange(File file) { 
     called = true; 
    } 
} 

... 
TestPropertyFileWatcher watcher = new TestPropertyFileWatcher 
... 
assertTrue(watcher.called); 
7

다음은 테스트를위한 간단한 수정입니다.

@Test 
public void testPropertyFileWatcher() throws Exception { 
    final File file = new File("testfile"); 
    file.createNewFile(); 

    final AtomicBoolean hasCalled = new AtomicBoolean(); 
    PropertyFileWatcher propertyFileWatcher = 
     new PropertyFileWatcher(file) 
     { 
     protected void onChange (final File localFile) 
     { 
      hasCalled.set(true); 
      assertEquals(file, localFile); 
     } 
     } 


    Timer timer = new Timer(); 
    timer.schedule(propertyFileWatcher, 2000); 

    FileWriter fw = new FileWriter(file); 
    fw.write("blah"); 
    fw.close(); 

    Thread.sleep(8000); 
    // check if propertyFileWatcher.onChange was called 

    assertTrue(hasCalled.get()); 
    file.delete(); 
} 
+0

조롱 프레임 워크에 종속성을 추가하지 않기 때문에이 솔루션을 정말 좋아합니다. 그러나 유닛 테스트를 위해서는 조롱 프레임 워크가 필요합니다. 그래서 내가 조롱하는 제안을 제 질문에 대한 대답으로 받아들이고 있습니다. – nkr1pt

+0

@ nkr1pt. 확실히 평판이 좋은 조롱 프레임 워크와 함께 가십시오. 1.4 버전의 JDK에 국한되지 않은 경우 jMock을 살펴보십시오. –

관련 문제