使用powermock和Jersey Test + TestNg测试Jersey休息服务时的问题

编程入门 行业动态 更新时间:2024-10-19 08:43:58
本文介绍了使用powermock和Jersey Test + TestNg测试Jersey休息服务时的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

开发了使用Jersey的休息服务。现在,我想为此Web服务编写一些集成测试,但是由于不是已经实现了Web服务中使用的每个类,因此我需要模拟其中的一些。例如,我有以下类:

A rest service using Jersey developed. Now I want to write some integration tests for this web service but since not every class being used from the web service is already implemented I need to mock some of them. For example I have the following class:

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(); }}

然后我要测试使用Jersey测试框架和TestNg进行集成测试Powerpock(使用构造函数模拟新对象)。

Then i want to test do integration test with Jersey test framework and TestNg + Powermock (mock new object with constructor).

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"); } }

我的pom依赖:

<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>

但是在运行时,getIt()方法中的实例ServiceA仍被初始化为新的ServiceA对象,而不是模拟对象。

But in runtime, the instance ServiceA in getIt() method is still initalized as new ServiceA object not the mockA object.

对此有任何想法吗?谢谢,

Any idea about it? Thanks,

如果添加PowerMockIgnore({ javax.ws。, org.glassfish。}),则请参见以下异常:

if add PowerMockIgnore({"javax.ws.", "org.glassfish."}), then see below exception:

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

推荐答案

我相信模拟可能不会发生,因为您没有Po

I believe the mock is perhaps not happening because you dont have the PowerMock's objectfactory implementation wired into TestNG.

为了使powermockito和TestNG相互配合,您必须执行以下操作:

In order to get powermockito and TestNG to work with each other you have to do the following :

  • 配置TestNG以使用PowerMock对象工厂:您可以通过 object-factory 中的属性来执行此操作套件xml的c $ c>< suite> 标记,或通过 @ org.testng.annotations.ObjectFactory 带注释的方法返回Powermock的TestNG接口 org.testng.IObjectFactory 的实现,即 org.powermock.modules.testng.PowerMockObjectFactory (或)通过扩展 org.powermock.modules.testng.PowerMockTestCase
  • 使用@PrepareForTest来准备要由PowerMockit模拟的静态类
  • Configure TestNG to use the PowerMock object factory : You can do this either via the attribute object-factory in your <suite> tag of your suite xml or via an @org.testng.annotations.ObjectFactory annotated method which returns the powermock's implementation of TestNG interface org.testng.IObjectFactory viz., org.powermock.modules.testng.PowerMockObjectFactory (or) by extending org.powermock.modules.testng.PowerMockTestCase
  • Use @PrepareForTest to prepare your static class for being mocked by PowerMockit

我在代码中使用PowerMock的Object工厂接线找不到对您的引用。您可以尝试这样做,看看是否有帮助吗?

I couldnt find any reference to you using the PowerMock's Object factory wiring in, in your code. Can you please try doing that and see if that helps ?

请在此处中查看有关完整的详细信息。

Please see here for full details.

更新: 为了摆脱ClassCastExceptions,您需要添加以下内容

Update: In order to get past the ClassCastExceptions you would need to add the below annotation to your class.

@org.powermock.core.classloader.annotations.PowerMockIgnore({"javax.ws.*", "org.glassfish.*"})

有关此操作的更多详细信息,请参阅这个stackoverflow 问题

For more details around this, please refer to this stackoverflow question

PS:我假设您正在使用以下注释使powermock为模拟准备测试类(您的问题示例中显示了其他内容)

PS : I am assuming that you are using the following annotation to have powermock prepare your test class for mocking (Your sample in your question is showing something else)

@org.powermock.core.classloader.annotations.PrepareForTest({ResourceController.class})

更多推荐

使用powermock和Jersey Test + TestNg测试Jersey休息服务时的问题

本文发布于:2023-10-31 04:38:02,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1545030.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:测试   Jersey   powermock   TestNg   Test

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!