11package hudson .plugins .scm_sync_configuration .util ;
22
3- import static org .easymock .EasyMock .expect ;
4- import static org .easymock .EasyMock .notNull ;
5- import static org .powermock .api .easymock .PowerMock .createPartialMock ;
6- import static org .powermock .api .easymock .PowerMock .mockStatic ;
7- import static org .powermock .api .easymock .PowerMock .replay ;
3+ import static org .mockito .Mockito .spy ;
4+ import static org .mockito .Mockito .when ;
85import hudson .Plugin ;
96import hudson .PluginWrapper ;
107import hudson .model .Hudson ;
2118import java .io .File ;
2219import java .io .IOException ;
2320import java .lang .reflect .Field ;
24- import java .util .ArrayList ;
25- import java .util .List ;
2621import java .util .regex .Pattern ;
2722
2823import org .codehaus .plexus .PlexusContainerException ;
2924import org .codehaus .plexus .component .repository .exception .ComponentLookupException ;
3025import org .codehaus .plexus .util .FileUtils ;
31- import org .easymock .EasyMock ;
3226import org .junit .After ;
3327import org .junit .Before ;
3428import org .junit .Rule ;
3529import org .junit .rules .TestName ;
3630import org .junit .runner .RunWith ;
37- import org .powermock .api .easymock .PowerMock ;
31+ import org .mockito .Mockito ;
32+ import org .objenesis .ObjenesisStd ;
33+ import org .powermock .api .mockito .PowerMockito ;
3834import org .powermock .core .classloader .annotations .PrepareForTest ;
3935import org .powermock .modules .junit4 .PowerMockRunner ;
4036import org .springframework .core .io .ClassPathResource ;
@@ -54,8 +50,8 @@ public void setup() throws Throwable {
5450 ScmSyncConfigurationPlugin scmSyncConfigPluginInstance = new ScmSyncConfigurationPlugin ();
5551
5652 // Mocking PluginWrapper attached to current ScmSyncConfigurationPlugin instance
57- PluginWrapper pluginWrapper = PowerMock . createMock (PluginWrapper .class );
58- expect (pluginWrapper .getShortName ()).andStubReturn ("scm-sync-configuration" );
53+ PluginWrapper pluginWrapper = PowerMockito . mock (PluginWrapper .class );
54+ when (pluginWrapper .getShortName ()).thenReturn ("scm-sync-configuration" );
5955 // Setting field on current plugin instance
6056 Field wrapperField = Plugin .class .getDeclaredField ("wrapper" );
6157 boolean wrapperFieldAccessibility = wrapperField .isAccessible ();
@@ -78,19 +74,20 @@ public void setup() throws Throwable {
7874 FileUtils .copyDirectoryStructure (new ClassPathResource ("svnEmptyRepository" ).getFile (), curentLocalSvnRepository );
7975
8076 // Mocking user
81- User mockedUser = EasyMock . createMock (User .class );
82- expect (mockedUser .getId ()).andStubReturn ("fcamblor" );
77+ User mockedUser = Mockito . mock (User .class );
78+ when (mockedUser .getId ()).thenReturn ("fcamblor" );
8379
8480 // Mocking Hudson singleton instance ...
85- mockStatic ( Hudson . class );
86- Hudson hudsonMockedInstance = createPartialMock ( Hudson . class , new String []{ "getRootDir" , "getMe" , "getPlugin" });
87- expect ( Hudson . getInstance ()). andStubReturn ( hudsonMockedInstance );
88- expect ( hudsonMockedInstance . getRootDir ()). andStubReturn ( currentHudsonRootDirectory );
89- expect ( hudsonMockedInstance . getMe ()). andStubReturn ( mockedUser );
90- expect ( hudsonMockedInstance .getPlugin (ScmSyncConfigurationPlugin .class )). andStubReturn ( scmSyncConfigPluginInstance );
81+ // Warning : this line will only work on Objenesis supported VMs :
82+ // http://code.google.com/p/objenesis/wiki/ListOfCurrentlySupportedVMs
83+ Hudson hudsonMockedInstance = spy (( Hudson ) new ObjenesisStd (). getInstantiatorOf ( Hudson . class ). newInstance () );
84+ PowerMockito . doReturn ( currentHudsonRootDirectory ). when ( hudsonMockedInstance ). getRootDir ( );
85+ PowerMockito . doReturn ( mockedUser ). when ( hudsonMockedInstance ). getMe ( );
86+ PowerMockito . doReturn ( scmSyncConfigPluginInstance ). when ( hudsonMockedInstance ) .getPlugin (ScmSyncConfigurationPlugin .class );
9187
92- replay (hudsonMockedInstance , pluginWrapper , mockedUser );
93- replay (Hudson .class );
88+ PowerMockito .mockStatic (Hudson .class );
89+ PowerMockito .doReturn (hudsonMockedInstance ).when (Hudson .class ); Hudson .getInstance ();
90+ //when(Hudson.getInstance()).thenReturn(hudsonMockedInstance);
9491 }
9592
9693 @ After
@@ -113,24 +110,13 @@ protected static File createTmpDirectory(String directoryPrefix) throws IOExcept
113110
114111 protected SCM createSCMMock (boolean withCredentials ){
115112
116- List <String > partiallyMockedMethods = new ArrayList <String >();
117- if (withCredentials ){
118- partiallyMockedMethods .add ("extractScmCredentials" );
119- }
120-
121- SCM mockedSCM = createPartialMock (getSCMClass (), partiallyMockedMethods .toArray (new String [0 ]));
122- mockStatic (SCM .class );
113+ SCM mockedSCM = spy (SCM .valueOf (getSCMClass ().getName ()));
123114
124- expect (SCM .valueOf (notNull (String .class ))).andReturn (mockedSCM );
125-
126115 if (withCredentials ){
127116 SCMCredentialConfiguration mockedCredential = new SCMCredentialConfiguration ("toto" );
128- expect ( mockedSCM .extractScmCredentials (notNull (String . class ))). andReturn ( mockedCredential ). anyTimes ( );
117+ PowerMockito . doReturn ( mockedCredential ). when ( mockedSCM ) .extractScmCredentials ((String ) Mockito . notNull () );
129118 }
130119
131- replay (mockedSCM );
132- replay (SCM .class );
133-
134120 return mockedSCM ;
135121 }
136122
0 commit comments