public class ServiceA {
public String getService() {
return null;
}}
@Path("/myresource")
public class ResourceController {
@GET
@Produces("text/plain")
public String getIt() {
ServiceA a = new ServiceA();
return a.getService();
}}
import org.glassfish.jersey.server.ResourceConfig;
import org.glassfish.jersey.test.JerseyTestNg;
import org.glassfish.jersey.test.jdkhttp.JdkHttpServerTestContainerFactory;
import org.glassfish.jersey.test.spi.TestContainerFactory;
import org.mockito.Mockito;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PowerMockIgnore;
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;
import javax.ws.rs.core.Application;
import javax.ws.rs.core.Response;
import static org.powermock.api.mockito.PowerMockito.mock;
@PowerMockIgnore({"javax.ws.*", "org.glassfish.*"})
@PrepareForTest({ResourceController.class})
public class ResourceControllerTest extends JerseyTestNg.ContainerPerMethodTest {
@Override
protected Application configure() {
return new ResourceConfig(ResourceController.class);
}
@Override
protected TestContainerFactory getTestContainerFactory() {
return new JdkHttpServerTestContainerFactory();
}
@ObjectFactory
public IObjectFactory getObjectFactory() {
return new PowerMockObjectFactory();
}
@Test
public void getItTest() {
ServiceA mockA = mock(ServiceA.class);
Mockito.when(mockA.getService()).thenReturn("service a");
PowerMockito.whenNew(ServiceA.class).withAnyArguments().thenReturn(mockA);
Response result = target("myresource").request().get();
Assert.assertEquals(result.readEntity(String.class), "service a");
}
}
<dependency>
<groupId>org.glassfish.jersey.test-framework.providers</groupId>
<artifactId>jersey-test-framework-provider-inmemory</artifactId>
<version>2.21</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.test-framework</groupId>
<artifactId>jersey-test-framework-core</artifactId>
<version>2.21</version>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-module-testng</artifactId>
<version>1.7.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-api-mockito</artifactId>
<version>1.7.0</version>
<scope>test</scope>
</dependency>
如果添加PowerMockIgnore({“javax.ws.”,“org.glassfish.”}),请参见下面的异常:
java.lang.ExceptionInInitializerError
at org.mockito.internal.exceptions.stacktrace.ConditionalStackTraceFilter.<init>(ConditionalStackTraceFilter.java:17)
at org.mockito.exceptions.base.MockitoException.filterStackTrace(MockitoException.java:41)
at org.mockito.exceptions.base.MockitoException.<init>(MockitoException.java:30)
at org.mockito.exceptions.misusing.MockitoConfigurationException.<init>(MockitoConfigurationException.java:18)
at org.mockito.internal.configuration.plugins.PluginLoader.loadImpl(PluginLoader.java:66)
at org.mockito.internal.configuration.plugins.PluginLoader.loadPlugin(PluginLoader.java:24)
at org.mockito.internal.configuration.plugins.PluginRegistry.<init>(PluginRegistry.java:12)
at org.mockito.internal.configuration.plugins.Plugins.<clinit>(Plugins.java:11)
at org.mockito.internal.util.MockUtil.<clinit>(MockUtil.java:24)
at org.mockito.internal.configuration.injection.scanner.MockScanner.<init>(MockScanner.java:22)
at org.mockito.internal.configuration.InjectingAnnotationEngine.injectMocks(InjectingAnnotationEngine.java:96)
at org.powermock.api.mockito.internal.configuration.PowerMockitoInjectingAnnotationEngine.process(PowerMockitoInjectingAnnotationEngine.java:35)
at org.powermock.api.extension.listener.AnnotationEnabler.injectSpiesAndInjectToSetters(AnnotationEnabler.java:72)
at org.powermock.api.extension.listener.AnnotationEnabler.beforeTestMethod(AnnotationEnabler.java:64)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.powermock.reflect.internal.WhiteboxImpl.performMethodInvocation(WhiteboxImpl.java:1846)
at org.powermock.reflect.internal.WhiteboxImpl.invokeMethod(WhiteboxImpl.java:700)
at org.powermock.reflect.Whitebox.invokeMethod(Whitebox.java:415)
at org.powermock.modules.testng.internal.PowerMockTestNGMethodHandler.injectMocksUsingAnnotationEnabler(PowerMockTestNGMethodHandler.java:76)
at org.powermock.modules.testng.internal.PowerMockTestNGMethodHandler.invoke(PowerMockTestNGMethodHandler.java:48)
at com.hp.ucmdb.rest.ResourceControllerTest_$$_jvstb11_0.setUp(ResourceControllerTest_$$_jvstb11_0.java)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:86)
at org.testng.internal.Invoker.invokeConfigurationMethod(Invoker.java:514)
at org.testng.internal.Invoker.invokeConfigurations(Invoker.java:215)
at org.testng.internal.Invoker.invokeMethod(Invoker.java:589)
at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:820)
at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1128)
at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:129)
at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:112)
at org.testng.TestRunner.privateRun(TestRunner.java:782)
at org.testng.TestRunner.run(TestRunner.java:632)
at org.testng.SuiteRunner.runTest(SuiteRunner.java:366)
at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:361)
at org.testng.SuiteRunner.privateRun(SuiteRunner.java:319)
at org.testng.SuiteRunner.run(SuiteRunner.java:268)
at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
at org.testng.TestNG.runSuitesSequentially(TestNG.java:1244)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1169)
at org.testng.TestNG.run(TestNG.java:1064)
at org.testng.IDEARemoteTestNG.run(IDEARemoteTestNG.java:72)
at org.testng.RemoteTestNGStarter.main(RemoteTestNGStarter.java:127)
Caused by: java.lang.NullPointerException
at org.mockito.internal.configuration.plugins.Plugins.getStackTraceCleanerProvider(Plugins.java:17)
at org.mockito.internal.exceptions.stacktrace.StackTraceFilter.<clinit>(StackTraceFilter.java:21)
... 49 more
我相信模拟可能不会发生,因为您没有将PowerMock的objectfactory实现连接到TestNG中。
为了使powermockito和TestNG相互协作,您必须执行以下操作:
标记中的属性Object-Factory
或通过@org.TestNG.annotations.objectFactory
注释方法执行此操作,该方法返回PowerMock对TestNG接口的实现org.TestNG.iobjectFactory
,即 更新:为了通过ClassCastExceptions,您需要向您的类添加以下注释。
@org.powermock.core.classloader.annotations.PowerMockIgnore({"javax.ws.*", "org.glassfish.*"})
有关这方面的更多详细信息,请参阅这个stackoverflow问题
PS:我假设您正在使用下面的注释来让powermock准备测试类进行模拟(您的问题中的示例显示了其他内容)
@org.powermock.core.classloader.annotations.PrepareForTest({ResourceController.class})
问题内容: 我用GET,POST,UPDATE和DELETE四种方法创建了Rest服务。这些方法建立了到数据库的连接以检索和存储数据。 现在,我想测试每种方法。我为此使用了Jersey测试框架。只要删除代码,真正对数据库进行调用的代码就可以正常工作。当我留下进行数据库调用的代码时,它引发了一个异常,即它无法连接到数据库。 编辑:我做了一些研究,并使用了依赖注入。数据库调用移到一个单独的类,但是我仍
我正在使用带有 netflix 堆栈和 Spring 启动的微服务构建一个应用程序。困扰我的一件事是,我还没有集成测试,我可以在其中模拟周围的服务。 因此,我有一个服务 A,它是一个带有功能区的尤里卡客户端,用于在呼叫期间将尤里卡名称解析为已注册服务 B 的 URL。 所以理想情况下,我想用 spring boot 的 integrationtest 注释启动应用程序,使用 wiremock 模拟
但现在,有些服务会变成空的。是否有方法在@Runwith注释中同时使用SpringJunit4ClassRunner.class和PowerMockRunner.class
我正在尝试使用TestNg并行运行测试。我有3个类(两个扩展了BaseTest类的测试类) BaseTest类只有“设置”和“拆卸”,没有其他东西。 当我尝试像这样并行运行测试时,一个带有测试的类运行,而另一个类只打开一个浏览器(但不执行测试) 但是当我从BaseTest类中剪切代码并将其直接放到我的每个测试类中(因此不扩展BaseTClass)时,代码工作,测试并行运行 为什么?我根本没有做任何
在本教程中,我们将演示如何在TestNG中执行超时测试。 “超时”表示如果单元测试花费的时间超过指定的毫秒数,那么TestNG将会中止它并将其标记为失败。 “超时”也可用于性能测试,以确保方法在合理的时间内返回。 创建一个名称为:TimeoutTest 的 Maven 项目,其结构如下所示 - 创建一个测试类:TestTimeout.java,其代码如下 - 执行上面代码,得到以下结果 -
我正在使用JUnit测试一个DAO类,我得到了一个nullpointerexception,我不知道为什么要启动服务类。以下是测试类: 以下是UserDaoImpl 如何在测试类中注入类?我想nullpointerxeception的原因是dao类没有正确地注入到测试类中