Mockito注解基本例子

Mockito的基本注解,以下代码是我测试一个复杂的消息驱动代码的测试逻辑。

/**
 * Created by jinglongjun on 16/1/25.
 */
public class TradeStatusChangeListenerTest extends TestCase {
    @Mock
    MergeSplitService mergeSplitService;
    @Mock
    TradeSearchService tradeSearchService;
    @Mock
    ITradeService tradeService;
    @Mock
    ITbTradeDao tbTradeDao;
    @Mock
    ISolrUpdateService solrUpdateService;
    @Mock
    PlatformTransactionManager txManager;
    @Spy
    TradeStatusChangeListener tcl;
    private User user;
    private TbTrade tbTrade;
    private List<TbTrade> result = new ArrayList<TbTrade>();
    @Before
    public void setUp() {
        MockitoAnnotations.initMocks(this);
        tcl.tradeSearchService = this.tradeSearchService;
        tcl.tradeService = this.tradeService;
        user = new User().setTaobaoId(1L);
        tbTrade = new TbTrade().setSysTid(1111L).setMergeSplitType(TbTrade.SPLIT_TYPE_AUTO_MERGE);
        result.add(tbTrade);
    }
    //测试退款关闭
    @Test
    public void testStatuschange() {
        EventSourceBase source = new CommonEventSource(new Object(), "12", "trade.close.statuschange",
                new Object[]{user}, result, "asdasd");
        tcl.onObserved(source);
        Mockito.verify(tradeSearchService, Mockito.atLeastOnce()).queryBySysTid(user, tbTrade.getSysTid(), true);
    }
}

你可能感兴趣的:(mockito)