是否可以使用Mockito在单位测试或Arquilian测试中而是在真实的Web容器中模拟界面?
我想做的是从IBM FileNet API:文档和属性中模拟这两个接口,以测试在容器(Wildfly(中运行的弹簧应用程序,而无需与IBM/FILENET框架进行实际连接。
UNITSEST正常工作,例如:
@Test
public void mock() {
Document d = Mockito.mock(Document.class);
Properties p = Mockito.mock(Properties.class);
Mockito.when(d.getProperties()).thenReturn(p);
Mockito.when(p.getStringValue("Ala")).thenReturn("Bella");
....
....
}
但是,当我试图将此代码部署到容器中时:
@Override
public DocumentSet someMethod(String ..... ) {
Document d = mock(Document.class); // this line throws an Exception
Properties p = mock(Properties.class);
when(d.getProperties()).thenReturn(p);
.....
我在此行中获得ClassNotFoundException
例外:Document d = mock(Document.class);
1:18:11,310 ERROR [org.springframework.batch.core.step.AbstractStep] (SimpleAsyncTaskExecutor-1) Encountered an error executing step csvFilesProcessStep in job process_csv_files: java.lang.NoClassDefFoundError: org/mockito/Mockito
at com.somepackage.step.ce.CeOperationsProcessorTestImpl.searchAndFetchDocumentsById(CeOperationsProcessorTestImpl.java:44)
at com.somepackage.step.CvsFilesProcessor.processFile(CvsFilesProcessor.java:110)
at com.somepackage.step.CvsFilesProcessor.process(CvsFilesProcessor.java:83)
at com.somepackage.step.CvsFilesProcessor.process(CvsFilesProcessor.java:1)
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.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:317)
at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:190)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157)
at org.springframework.aop.support.DelegatingIntroductionInterceptor.doProceed(DelegatingIntroductionInterceptor.java:133)
at org.springframework.aop.support.DelegatingIntroductionInterceptor.invoke(DelegatingIntroductionInterceptor.java:121)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:207)
at com.sun.proxy.$Proxy62.process(Unknown Source)
at org.springframework.batch.core.step.item.SimpleChunkProcessor.doProcess(SimpleChunkProcessor.java:126)
at org.springframework.batch.core.step.item.SimpleChunkProcessor.transform(SimpleChunkProcessor.java:293)
at org.springframework.batch.core.step.item.SimpleChunkProcessor.process(SimpleChunkProcessor.java:192)
at org.springframework.batch.core.step.item.ChunkOrientedTasklet.execute(ChunkOrientedTasklet.java:75)
at org.springframework.batch.core.step.tasklet.TaskletStep$ChunkTransactionCallback.doInTransaction(TaskletStep.java:406)
at org.springframework.batch.core.step.tasklet.TaskletStep$ChunkTransactionCallback.doInTransaction(TaskletStep.java:330)
at org.springframework.transaction.support.TransactionTemplate.execute(TransactionTemplate.java:133)
at org.springframework.batch.core.step.tasklet.TaskletStep$2.doInChunkContext(TaskletStep.java:271)
at org.springframework.batch.core.scope.context.StepContextRepeatCallback.doInIteration(StepContextRepeatCallback.java:77)
at org.springframework.batch.repeat.support.RepeatTemplate.getNextResult(RepeatTemplate.java:368)
at org.springframework.batch.repeat.support.RepeatTemplate.executeInternal(RepeatTemplate.java:215)
at org.springframework.batch.repeat.support.RepeatTemplate.iterate(RepeatTemplate.java:144)
at org.springframework.batch.core.step.tasklet.TaskletStep.doExecute(TaskletStep.java:257)
at org.springframework.batch.core.step.AbstractStep.execute(AbstractStep.java:198)
at org.springframework.batch.core.job.SimpleStepHandler.handleStep(SimpleStepHandler.java:148)
at org.springframework.batch.core.job.flow.JobFlowExecutor.executeStep(JobFlowExecutor.java:64)
at org.springframework.batch.core.job.flow.support.state.StepState.handle(StepState.java:67)
at org.springframework.batch.core.job.flow.support.SimpleFlow.resume(SimpleFlow.java:165)
at org.springframework.batch.core.job.flow.support.SimpleFlow.start(SimpleFlow.java:144)
at org.springframework.batch.core.job.flow.FlowJob.doExecute(FlowJob.java:134)
at org.springframework.batch.core.job.AbstractJob.execute(AbstractJob.java:304)
at org.springframework.batch.core.launch.support.SimpleJobLauncher$1.run(SimpleJobLauncher.java:135)
at java.lang.Thread.run(Thread.java:748)
Caused by: java.lang.ClassNotFoundException: org.mockito.Mockito from [Module "deployment.someapp_web-1.1.3-SNAPSHOT.war:main" from Service Module Loader]
at org.jboss.modules.ModuleClassLoader.findClass(ModuleClassLoader.java:198)
at org.jboss.modules.ConcurrentClassLoader.performLoadClassUnchecked(ConcurrentClassLoader.java:363)
at org.jboss.modules.ConcurrentClassLoader.performLoadClass(ConcurrentClassLoader.java:351)
at org.jboss.modules.ConcurrentClassLoader.loadClass(ConcurrentClassLoader.java:93)
... 39 more
目前,我实现了这两个接口的测试实现,但这很乏味,因为它们有数百种方法。
很可能,您将Mockito作为Maven中的测试依赖性(或用于构建代码的任何内容(。在pom.xml
文件中,在<dependency>
标签中将有一个线路<scope>test</scope>
,其中overito被导入。如果您评论该行,则应该能够找到该课程。另外,Wildfly需要Mockito Jar文件,因此请确保它在战争文件中或LIB目录中。
但是...您不是想在生产服务器上进行集成测试吗?我一生无法想象你为什么想做这样的事情。这有点工作,但是您想做的就是编写一个集成测试,该测试启动您的服务器,部署代码,测试并关闭服务器。为java integration test
做Google。