如何模拟UUID?

时间:2019-02-07 09:35:25

标签: java junit4 powermockito

可以模拟UUID吗?或我的源代码有任何问题?

看看例子:

  1. MyTest类

     @RunWith(PowerMockRunner.class)
     @PrepareForTest({UUID.class,ActualClass.class,MyTest.class})
     public class MyTest extends AbstractMuleContextTestCase{
     ActualClass obj=new ActualClass ();
     @Before
     public void setUp() throws Exception {
     final String id = "2f2ae98b-8fb2-4e2f-9913-65eaabdbcc29";
     ActualClass instance = PowerMockito.spy(new ActualClass ());
     PowerMockito.when(instance, 
     UUID.randomUUID().toString()).thenReturn(id);
       }
     }
    
  2. MyMain类

    Class MyMain implements Callable {
    public Object onCall(MuleEventContext eventContext) throws Exception {
             //some function
    private String updatingUpdateQuery(){
    String uid=UUID.randomUUID().toString();
    //some function
      }
    }
    

如何模拟String uid?

我遇到以下错误

     java.lang.IllegalStateException: Failed to transform class with name com.MyTest  Reason: 3
        at org.powermock.core.classloader.MockClassLoader.loadMockClass(MockClassLoader.java:265)
        at org.powermock.core.classloader.MockClassLoader.loadModifiedClass(MockClassLoader.java:179)
        at org.powermock.core.classloader.DeferSupportingClassLoader.loadClass(DeferSupportingClassLoader.java:70)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        at java.lang.Class.forName0(Native Method)
        at java.lang.Class.forName(Unknown Source)
        at org.powermock.modules.junit4.common.internal.impl.JUnit4TestSuiteChunkerImpl.createDelegatorFromClassloader(JUnit4TestSuiteChunkerImpl.java:145)
        at org.powermock.modules.junit4.common.internal.impl.JUnit4TestSuiteChunkerImpl.createDelegatorFromClassloader(JUnit4TestSuiteChunkerImpl.java:40)
        at org.powermock.tests.utils.impl.AbstractTestSuiteChunkerImpl.createTestDelegators(AbstractTestSuiteChunkerImpl.java:244)
        at org.powermock.modules.junit4.common.internal.impl.JUnit4TestSuiteChunkerImpl.<init>(JUnit4TestSuiteChunkerImpl.java:61)
        at org.powermock.modules.junit4.common.internal.impl.AbstractCommonPowerMockRunner.<init>(AbstractCommonPowerMockRunner.java:32)
        at org.powermock.modules.junit4.PowerMockRunner.<init>(PowerMockRunner.java:34)
        at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
        at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)

1 个答案:

答案 0 :(得分:0)

在我的情况下,我使用的是JUnit 5,所以我无法使用PowerMock,并且Mockito不提供静态方法模拟。我重新编写了一些生产代码,因此不需要模拟UUID静态方法。

我使用了供应商界面

public class TestedClass {
    private final Supplier<UUID> uuidSupplier = UUID::randomUUID;

    public String getUuid() {
        return uuidSupplier.get().toString();
    }
}

然后我使用反射来对其进行测试

public class TestedClassTest {
    @Test
    public void testMethod() throws NoSuchFieldException, IllegalAccessException {
        Supplier<UUID> uuidSupplier = mock(Supplier.class);
        TestedClass testedClass = new TestedClass();
        Field uuidSupplierField = TestedClass.class.getDeclaredField("uuidSupplier");
        uuidSupplierField.setAccessible(true);
        uuidSupplierField.set(testedClass, uuidSupplier);

        String uuid = "5211e915-c3e2-4dcb-0776-c7b900f38ab7";
        when(uuidSupplier.get()).thenReturn(UUID.fromString(uuid));

        assertEquals(uuid, testedClass.getUuid());
    }
}

根据问题的作者的建议,这是我的进口商品:

import org.junit.jupiter.api.Test;

import java.lang.reflect.Field;
import java.util.UUID;
import java.util.function.Supplier;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

这里是我的依赖项:

<dependency>
    <groupId>org.mockito</groupId>
    <artifactId>mockito-core</artifactId>
    <version>1.10.19</version>
    <scope>test</scope>
</dependency>
<dependency>
    <groupId>org.junit.jupiter</groupId>
    <artifactId>junit-jupiter-engine</artifactId>
    <version>5.2.0</version>
    <scope>test</scope>
</dependency>

希望它对其他人有帮助。