How to clean up mocks in spring tests when using Mockito

前端 未结 4 2009
孤城傲影
孤城傲影 2020-12-25 11:30

I\'m pretty new to Mockito and have some trouble with clean up.

I used to use JMock2 for unit tests. As far as I know, JMock2 preserves the expectations and other mo

4条回答
  •  南方客
    南方客 (楼主)
    2020-12-25 12:21

    Instead of injecting the placeOrderService object, you should probably just let Mockito initialize it as a @Mock before each test, via something like this:

    @Mock private PlaceOrderService placeOrderService;
    
    @Before
    public void setup() {
        MockitoAnnotations.initMocks(this);
    }
    

    As recommended in the Javadoc here: http://docs.mockito.googlecode.com/hg/latest/org/mockito/MockitoAnnotations.html

    You can even put the @Before method in a superclass and just extend it for each test case class that uses @Mock objects.

提交回复
热议问题