是否可以模拟UUID?或我的源代码中的任何问题?
看一下例子:
@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);
}
}
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)
就我而言,我使用的是JUnit 5,因此无法使用PowerMock,Mockito也没有提供静态方法模拟。我重写了一下我的生产代码,这样我就不需要模拟UUID静态方法了。
我使用了Supplier接口
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>
希望它可以帮助别人。