gpt4 book ai didi

mocking - PowerMockMockito 测试在 Junit 下运行但不在 TestNG 下运行

转载 作者:行者123 更新时间:2023-12-01 09:32:57 26 4
gpt4 key购买 nike

我正在尝试运行 this baseline unit test来自 Powermock。使用 Mockito 和 Junit 从该链接中按原样运行代码,并且它运行时没有错误。不幸的是,我需要 Mockito 和 TestNG,这让我在 10 次测试中有 6 次出现错误:

Mockito cannot mock this class: class replica.java.util.UUID$$PowerMock5
Mockito can only mock visible & non-final classes.

抱歉,这是一个很长的代码帖子,但我想要完整。第一个测试通过,第二个、第 5 个、第 6 个、第 7 个、第 9 个和第 10 个都失败。这可以在TestNG下工作吗?

编辑 - 添加版本号

这些结果出现在 Mockito V1.9.0、Powermock V1.4.12 和 TestNG v6.8(均在 Java 1.7 下运行)

这是我稍作修改的代码:

/*
* Copyright 2008 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import static org.mockito.BDDMockito.given;
import static org.mockito.Matchers.anyLong;
import static org.mockito.Mockito.times;
import static org.powermock.api.mockito.PowerMockito.doNothing;
import static org.powermock.api.mockito.PowerMockito.mock;
import static org.powermock.api.mockito.PowerMockito.mockStatic;
import static org.powermock.api.mockito.PowerMockito.spy;
import static org.powermock.api.mockito.PowerMockito.verifyStatic;
import static org.powermock.api.mockito.PowerMockito.when;

import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import java.util.UUID;

import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.testng.PowerMockObjectFactory;
import org.testng.Assert;
import org.testng.IObjectFactory;
import org.testng.annotations.ObjectFactory;
import org.testng.annotations.Test;

/**
* Demonstrates PowerMockito's ability to mock non-final and final system
* classes. To mock a system class you need to prepare the calling class for
* testing. I.e. let's say you're testing class A which interacts with
* URLEncoder then you would do:
*
* <pre>
*
* &#064;PrepareForTest({A.class})
*
* </pre>
*/
// @RunWith(PowerMockRunner.class)
@PrepareForTest({ SystemClassUser.class })
public class SystemClassUserTest {

@ObjectFactory
public IObjectFactory getObjectFactory() {
return new PowerMockObjectFactory();
}

@Test
public void assertThatMockingOfNonFinalSystemClassesWorks() throws Exception {
mockStatic(URLEncoder.class);

when(URLEncoder.encode("string", "enc")).thenReturn("something");

Assert.assertEquals("something", new SystemClassUser().performEncode());

}

@Test
public void assertThatMockingOfTheRuntimeSystemClassWorks() throws Exception {
mockStatic(Runtime.class);

Runtime runtimeMock = mock(Runtime.class);
Process processMock = mock(Process.class);

when(Runtime.getRuntime()).thenReturn(runtimeMock);
when(runtimeMock.exec("command")).thenReturn(processMock);

Assert.assertSame(processMock, new SystemClassUser().executeCommand());
}

@Test
public void assertThatMockingOfFinalSystemClassesWorks() throws Exception {
mockStatic(System.class);

when(System.getProperty("property")).thenReturn("my property");

Assert.assertEquals("my property", new SystemClassUser().getSystemProperty());
}

@Test
public void assertThatPartialMockingOfFinalSystemClassesWorks() throws Exception {
spy(System.class);

when(System.nanoTime()).thenReturn(2L);

new SystemClassUser().doMoreComplicatedStuff();

Assert.assertEquals("2", System.getProperty("nanoTime"));
}

@Test
public void assertThatMockingOfCollectionsWork() throws Exception {
List<?> list = new LinkedList<Object>();
mockStatic(Collections.class);

Collections.shuffle(list);

new SystemClassUser().shuffleCollection(list);

verifyStatic(times(2));
Collections.shuffle(list);
}

@Test
public void assertThatPartialMockingOfFinalSystemClassesWorksForNonVoidMethods() throws Exception {
spy(System.class);

when(System.getProperty("property")).thenReturn("my property");

final SystemClassUser systemClassUser = new SystemClassUser();
systemClassUser.copyProperty("to", "property");
}

@Test
public void assertThatMockingStringWorks() throws Exception {
mockStatic(String.class);
final String string = "string";
final String args = "args";
final String returnValue = "returnValue";

when(String.format(string, args)).thenReturn(returnValue);

final SystemClassUser systemClassUser = new SystemClassUser();
Assert.assertEquals(systemClassUser.format(string, args), returnValue);
}

@Test
public void mockingStaticVoidMethodWorks() throws Exception {
mockStatic(Thread.class);
doNothing().when(Thread.class);
Thread.sleep(anyLong());

long startTime = System.currentTimeMillis();
final SystemClassUser systemClassUser = new SystemClassUser();
systemClassUser.threadSleep();
long endTime = System.currentTimeMillis();
Assert.assertTrue(endTime - startTime < 5000);
}

@Test
public void mockingURLWorks() throws Exception {
URL url = mock(URL.class);
URLConnection urlConnectionMock = mock(URLConnection.class);

when(url.openConnection()).thenReturn(urlConnectionMock);

URLConnection openConnection = url.openConnection();

Assert.assertSame(openConnection, urlConnectionMock);
}

@Test
public void mockingUUIDWorks() throws Exception {
// given
final UUID mock = mock(UUID.class);
mockStatic(UUID.class);
given(UUID.randomUUID()).willReturn(mock);

// when
String actual = new SystemClassUser().generatePerishableToken();

// then
Assert.assertEquals("00000000000000000000000000000000", actual);
}
}

如果有帮助,这里是一个执行失败测试之一的精简版本:
[我不确定我是否应该编辑问题,如果那是我应该做的,我很抱歉,但是它已经太长了,这种方法似乎更有意义。]

import static org.powermock.api.mockito.PowerMockito.mockStatic;
import static org.powermock.api.mockito.PowerMockito.when;

import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.testng.PowerMockObjectFactory;
import org.testng.Assert;
import org.testng.IObjectFactory;
import org.testng.annotations.ObjectFactory;
import org.testng.annotations.Test;

@PrepareForTest({ SystemClassUser.class })
public class SystemClassUserTest {

@ObjectFactory
public IObjectFactory getObjectFactory() {
return new PowerMockObjectFactory();
}

@Test
public void assertThatMockingOfFinalSystemClassesWorks() throws Exception {
mockStatic(System.class);

when(System.getProperty("property")).thenReturn("my property");

Assert.assertEquals("my property", new SystemClassUser().getSystemProperty());
}
}

现在这里是 SystemClassUser 类,同样被剥离:

import java.io.IOException;

public class SystemClassUser {

public String getSystemProperty() throws IOException {
return System.getProperty("property");
}

}

在此异常中运行结果:

失败:assertThatMockingOfFinalSystemClassesWorks
org.mockito.exceptions.base.MockitoException:
Mockito 不能模拟这个类:class replica.java.lang.System$$PowerMock0
Mockito 只能模拟可见和非最终类。

最佳答案

我只是将测试用例修改为

public class SystemClassUserTest extends PowerMockTestCase {
...
}

它工作正常

编辑(一年后):我最近发现 System Rules library ,我没试过 getProperty() ,但是 System.exit()完全按照我的预期工作......

关于mocking - PowerMockMockito 测试在 Junit 下运行但不在 TestNG 下运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13011090/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com