title: How to mock UUID.randomUUID() from java.util package?
body: <p>What is wrong with the <code>UUID.randomUUID()</code> - it just can't be mocked</p>
  
  <p>Is it possible to mock? Or i have an error in my source?</p>
  
  <p>Look at example:</p>
  
  <p>1) Class that is tested</p>
  
  <pre><code>package com.grayen;
  
  import java.util.UUID;
  
  public class TestedClass {
      public UUID getUuid() {
          return UUID.randomUUID();
      }
      public UUID getUuidFromWrapper() {
          return UuidWrapper.randomUUID();
      }
  }
  </code></pre>
  
  <p>One method uses a wrapper for UUID and i can mock that wrapper!</p>
  
  <p>2) Wrapper for real UUID (all modifiers the same)</p>
  
  <pre><code>package com.grayen;
  
  import java.util.UUID;
  
  public final class UuidWrapper {
      public static UUID randomUUID() {
          return UUID.randomUUID();
      }
  }
  </code></pre>
  
  <p>3) Testing (last commented line throws an exception)</p>
  
  <pre><code>package com.grayen;
  
  import org.junit.Test;
  import org.junit.runner.RunWith;
  import org.powermock.api.mockito.PowerMockito;
  import org.powermock.core.classloader.annotations.PrepareForTest;
  import org.powermock.modules.junit4.PowerMockRunner;
  import java.util.UUID;
  import static org.junit.Assert.assertEquals;
  
  @PrepareForTest({UUID.class, UuidWrapper.class})
  @RunWith(PowerMockRunner.class)
  public class TestedClassTest {
  
      @Test
      public void testMethod() {
          UUID uuid = UUID.randomUUID();
  
          PowerMockito.mockStatic(UUID.class);
          PowerMockito.mockStatic(UuidWrapper.class);
  
          PowerMockito.when(UUID.randomUUID()).thenReturn(uuid);
          PowerMockito.when(UuidWrapper.randomUUID()).thenReturn(uuid);
  
          TestedClass testedClass = new TestedClass();
  
          assertEquals(uuid, testedClass.getUuidFromWrapper());
          //assertEquals(uuid, testedClass.getUuid());
      }
  }
  </code></pre>