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 ()); }
}