Mockito Powermock static method @Spy Object method mock

Source: Internet
Author: User
Import Org.junit.Assert;
Import Org.junit.Before;
Import Org.junit.BeforeClass;
Import Org.junit.Test;
Import Org.junit.runner.RunWith;
Import Org.mockito.InjectMocks;
Import Org.mockito.Matchers;
Import Org.mockito.Mock;
Import org.mockito.MockitoAnnotations;
Import Org.powermock.api.mockito.PowerMockito;
Import Org.powermock.core.classloader.annotations.PowerMockIgnore;
Import Org.powermock.core.classloader.annotations.PrepareForTest;


Import Org.powermock.modules.junit4.PowerMockRunner; * Note, I use JUnit, not with testng/@RunWith (Powermockrunner.class) @PrepareForTest ({fileutils.class, Resthttpclient.class, Studenthonorutil.class}) @PowerMockIgnore ({"javax.management.*"}) public class Xxxserviceimpltest {@Mock Messageserv

Ice Messageservice;

@InjectMocks @Spy//@Spy decorated objects must be manually new X1serviceimpl X1service = new X1serviceimpl ();

@InjectMocks//@spy modified objects, can not be manually new, by Mockito frame maintenance X2serviceimpl x2service;


    Only once public static void Beforeclass () {} is invoked @BeforeClass//class initialization.@Before//Every time a TestMethod is run, it runs a public void before () {//A mock of the static method, which must be placed here//if placed in Beforeclass, the unit test does not pass
        , although it seems that all unit tests before this class, only run once on the line, but not actually powermockito.mockstatic (Fileutils.class);
        Powermockito.mockstatic (Resthttpclient.class);
        Powermockito.mockstatic (Studenthonorutil.class);
        Mockitoannotations.initmocks (this); The trip is arranged according to the actual situation. @Test public void Method1test () throws Exception { Mockito.doreturn (New Xxx ()). When (X1service). SomeMethod ()//note that for @spy objects, you must mock Mockito.when (x2service.somemethod (Matchers.anystring (), Matchers.anyint ()).

Thenreturn (New linkedlist<xobject> ());
    Mockito.verify (X2service, mockito.times (0)). SomeMethod (Matchers.anystring (), matchers.anystring ()); }
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.