Tag: powermock

Mockito模拟对象从方法内的方法调用中分配一个空值

我有一个课我需要用mockito测试。 以下是课程和Mockito考试。 dbBuilder.parse(file)总是返回null,因为它调用了几个类,而这些类又调用了jar文件中的几个方法。 即使我嘲笑了所有这些,它总是返回null。 我无法跟踪空值的来源。 我试图压制这些方法,但仍无用。 由于此方法调用返回null,因此doc值为null。 因此,doc调用getElementsByTagName方法,并且运行mockito测试失败并显示NullPointerException 。 在我需要测试的此方法中,此代码后面有几行代码。 我该如何解决这个问题? class DocumentClass{ public void docMethod(){ DocumentBuilder dbBuilder = new DocumentBuilder(); Document doc = new Document(); FileStream file = new FileStream(new File(some path)); doc = dbBuilder.parse(file); NodeList nodes = doc.getElementsByTagName(“documents”); } } @RunWith(PowerMockRunner.class) @PrepareForTest({Document.class,DocumentBuilder.class,FileStream.class}) public class TestDocument{ @Test public documentTest(){ DocumentBuilder dbBuilder = PowerMockito.mock(DocumentBuilder.class); Document […]

使用powermock对静态方法进行unit testing

我想为我的项目中的一些静态方法编写unit testing用例, 我的class级代码片段, Class Util{ public static String getVariableValue(String name) { if(isWindows()){ return some string… } else{ return some other string… } } public static boolean isWindows(){ if(os is windows) return true; else return false; } } 基本上,当isWindows()返回’false’时,我想为getVariableValue()编写unit testing用例。 我如何使用powermock写这个?

unit testing使用资源包的静态方法

我已经阅读了很多关于使用Powermock和Mockito的文章,并尝试了很多不同的方法,但我仍然无法找到unit testing以下静态方法的方法。 public static Map getEntries() { Map myEntriesMap = new TreeMap(); ResourceBundle myEntries = ResourceBundle.getBundle(ENTRIES_BUNDLE); Enumeration enumList = myEntries.getKeys(); String key = null; String value = null; while (enumList.hasMoreElements()) { key = enumList.nextElement().toString(); value = myEntries.getString(key); myEntriesMap.put(key, value); } return myEntriesMap; } 代码是包含大约30个这样的静态方法的(遗留)类的一部分,并且重构实际上不是一个选项。 类似地,在一些其他静态方法中,正在检索DB连接。 例如:如何模拟资源包ENTRIES_BUNDLE并对此方法进行unit testing? 我正在寻找一种可以普遍适用于所有静态方法的模式。

NoClassDefFoundError:使用PowerMock-OSGi的org / hamcrest / Matchers

当我运行我的测试作为OSGi PlugIn测试时,我得到org.hamcrest.Matchers的NoClassDefFoundError,但是当我将其作为普通JUnit运行时测试everthing按预期工作。 我正在使用OSMi版本的PowerMock,并在我的启动配置中包含所有必要的依赖项。 我做错了什么? 由于某些原因,似乎Testrunner没有看到这个class级。 编辑:我创建了一个简化的示例项目,并发现问题只出现在我的类声明中使用@PrepareForTest(XXX.class)时。 java.lang.NoClassDefFoundError: org/hamcrest/Matchers at eu.gemtec.commons.util.assertion.Assert.assertParamNotNull(Assert.java:107) at eu.gemtec.eagle.device.aastra.omaxi.core.system.model.impl.MessageHandleFactory.(MessageHandleFactory.java:72) at eu.gemtec.eagle.device.aastra.omaxi.core.system.model.impl.TestMessageHandleFactory.setUp(TestMessageHandleFactory.java:74) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:601) at org.junit.internal.runners.MethodRoadie.runBefores(MethodRoadie.java:132) at org.junit.internal.runners.MethodRoadie.runBeforesThenTestThenAfters(MethodRoadie.java:95) at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$PowerMockJUnit44MethodRunner.executeTest(PowerMockJUnit44RunnerDelegateImpl.java:294) at org.powermock.modules.junit4.internal.impl.PowerMockJUnit47RunnerDelegateImpl$PowerMockJUnit47MethodRunner.executeTestInSuper(PowerMockJUnit47RunnerDelegateImpl.java:127) at org.powermock.modules.junit4.internal.impl.PowerMockJUnit47RunnerDelegateImpl$PowerMockJUnit47MethodRunner.access$100(PowerMockJUnit47RunnerDelegateImpl.java:59) at org.powermock.modules.junit4.internal.impl.PowerMockJUnit47RunnerDelegateImpl$PowerMockJUnit47MethodRunner$LastRuleTestExecutorStatement.evaluate(PowerMockJUnit47RunnerDelegateImpl.java:148) at org.junit.rules.ExpectedException$ExpectedExceptionStatement.evaluate(ExpectedException.java:168) at org.powermock.modules.junit4.internal.impl.PowerMockJUnit47RunnerDelegateImpl$PowerMockJUnit47MethodRunner.executeTest(PowerMockJUnit47RunnerDelegateImpl.java:91) at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$PowerMockJUnit44MethodRunner.runBeforesThenTestThenAfters(PowerMockJUnit44RunnerDelegateImpl.java:282) at org.junit.internal.runners.MethodRoadie.runTest(MethodRoadie.java:86) at org.junit.internal.runners.MethodRoadie.run(MethodRoadie.java:49) at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.invokeTestMethod(PowerMockJUnit44RunnerDelegateImpl.java:207) at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.runMethods(PowerMockJUnit44RunnerDelegateImpl.java:146) at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$1.run(PowerMockJUnit44RunnerDelegateImpl.java:120) at org.junit.internal.runners.ClassRoadie.runUnprotected(ClassRoadie.java:33) at org.junit.internal.runners.ClassRoadie.runProtected(ClassRoadie.java:45) […]

使用mockito来存根最终方法

我需要使用一个拥有最终方法的模拟器。 所以我使用powermock但它不起作用 class B { public final int nb() { return 4; } } @RunWith(PowerMockRunner.class) @PrepareForTest(B.class) public class Exemple extends TestCase { @Test public void test() { B b = PowerMockito.mock(B.class); PowerMockito.when(b.nb()).thenReturn(5); final int actualState = b.nb(); assertEquals(5, actualState); } } 如果有人有解决方案,请提前谢谢

PowerMockito – 如何使用带有类型列表的whenNew()?

我希望PowerMockito在调用new ArrayList()时返回我的空数组Foo ,但我不知道如何构造语句。 具体来说,我希望new ArrayList()正常创建一个新列表。 ArrayList fooList = new ArrayList(); PowerMockito.whenNew(ArrayList.class).withParameterTypes(Foo.class).thenReturn(fooList); ^这里基本上是我的,但.withParameterTypes(Foo.class)不允许我跟随.thenReturn() 。 我唯一的选择是withArguments(firstArgument, additionalArguments) 。 这可能与PowerMock有关,如果是这样,我该如何构建它? 编辑: 好吧,潜在的问题是我需要得到我正在尝试测试的方法的结果,但我不得不模拟请求,并且列表被放置在我正在尝试测试的方法结束时的请求中。 inspectionAction.viewInspectionDetailsAjax(mapping, form, request, response); 这个方法从请求中提取了几个参数,这些参数被Mockito.mock(HttpServletRequest.class); )。 通常在我们的应用程序中,我们将数据放在会话级变量上。 但是,由于此方法一次被调用多次并且结果ajax进入页面,因此每条数据都存储在请求中: request.setAttribute(“inspectionAjaxDetails”, details); 所以当模拟request时,我需要一些方法来获取details ,这是一个类型化的ArrayList。

@PowerMockIgnore在项目级别

在Maven中运行时,我的powermock测试用例中出现以下错误: java.lang.LinkageError: loader constraint violation: loader (instance of org/powermock/core/classloader/MockClassLoader) previously initiated loading for a different type with name “javax/management/MBeanServer” 解决方案是添加注释 @PowerMockIgnore(“javax.management.*”) 问题是我有很多测试文件,我必须添加这个注释。 有没有办法在项目级别或maven中添加它? 谢谢

使用Powermock测试Spring控制器

我有一个测试特定控制器的类,它工作正常 @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = “classpath:config/test-applicationContext-config.xml”) @TestExecutionListeners({ WebContextTestExecutionListener.class, DependencyInjectionTestExecutionListener.class, DirtiesContextTestExecutionListener.class }) public class TestAdminController { //….. } 我使用了mockito.org上的Google的Mockito库来模拟我的底层bean。 现在我的问题是:我有一些类需要被嘲笑,但他们有final方法,谷歌的mockito似乎没有解决这个问题。 我的一位同事建议使用powermock.org上的Powermock 。 但它需要使用@RunWith(PowerMockRunner.class)注释来注释测试器类。 如果我使用这个,我必须删除注释@RunWith(SpringJUnit4ClassRunner.class) ,这将给我带来问题,因为不会创建Spring测试上下文。 我怎么能避免这种情况? 建议我配置使用PowerMockRule而不是@RunWith注释 我的项目Maven依赖项如下 org.powermock powermock-mockito-release-full 1.5 pom org.powermock powermock-module-junit4-rule 1.5 test org.powermock powermock-classloading-xstream 1.5 test 现在我的class级看起来像这样(另一个控制器测试) @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = “classpath:config/test-applicationContext-config.xml”) @TestExecutionListeners({ WebContextTestExecutionListener.class, DependencyInjectionTestExecutionListener.class, DirtiesContextTestExecutionListener.class }) @PrepareForTest(ADSynchronizationImpl.class) public class ThirdPartyLoginControllerTest { @Rule public […]

使用PowerMockito模拟java.lang.Runtime

想为像这样的方法编写unit testing public static void startProgram() { process = Runtime.getRuntime().exec(command, null, file); } 我不想因为某些原因注入运行时对象,所以我想将getRuntime方法存根,它返回一个Runtime mock …我试过这样: @RunWith(PowerMockRunner.class) @PrepareForTest(Runtime.class) public class ProgramTest { @Test public void testStartProgram() { Runtime mockedRuntime = PowerMockito.mock(Runtime.class); PowerMockito.mockStatic(Runtime.class); Mockito.when(Runtime.getRuntime()).thenReturn(mockedRuntime); … //test } } 但这不起作用。 实际上似乎没有任何东西被嘲笑。 在测试中,使用正常的Runtime对象。 任何人都知道为什么这不起作用和/或它是如何工作的? 由于这个小例子似乎没有重现问题,这里是完整的测试代码:测试方法(缩短) public static synchronized long startProgram(String workspace) { // Here happens someting with Settings […]

如何模拟void方法抛出exception?

我有这样的结构: public class CacheWrapper { private Map innerMap; public CacheWrapper() { //initialize the innerMap with an instance for an in-memory cache //that works on external server //current implementation is not relevant for the problem innerMap = …; } public void putInSharedMemory(Object key, Object value) { innerMap.put(key, value); } public Object getFromSharedMemory(Object key) { return innerMap.get(key); […]