PROGRAMMING/JAVA

[Java] mockito spy void method

OJR 2011. 4. 8. 15:42

mockito spy void method

테스트 코드 작성중.

spy 를 사용해보고 있었다.


@Test
public void updateFoodList() throws Exception {

   ManagerListBO spyBO = spy(bo);
   ...
   doReturn(intakeFoodList).when(spyBO).getFoodList(id, data);
   doNothing().when(spyBO).calculateFood(id, date, foodList, foodIntake);
   ...
}

다른 사람 코드에서 테스트 코드를 만드는것이라서 원 소스는 건드리지 않고
테스트 코드에서 void method 일 경우 doNothing() 을 쓰면 되는구나.를 고생해서 알았다. ㅋ

 

 

 

public class MyClass {
    public void myVoidMethod(String arg1, int arg2) {
        // Some implementation here
    }
}
import static org.mockito.Mockito.*;

// ...

// Create a spy of MyClass
MyClass myClass = spy(new MyClass());

// Call the method with specific arguments
myClass.myVoidMethod("foo", 123);

// Use Mockito's doNothing() method to prevent the real method from being called
doNothing().when(myClass).myVoidMethod("foo", 123);

// Verify that the method was called with the specific arguments
verify(myClass).myVoidMethod("foo", 123);

 

반응형

'PROGRAMMING > JAVA' 카테고리의 다른 글

이클립스 (Eclipse)에서 로컬 톰캣띄우고 한글 파라미터 깨질 때  (0) 2012.11.11
HTML Entity 제거  (0) 2012.11.08
[java] 스프링  (0) 2011.03.20
[java] Cannot cast from Object to long  (0) 2010.11.24
java-lib  (0) 2010.08.20