Tag: powermock

测试是否调用了另一种方法

所以我确定那里有类似的东西,但我一直在寻找一个小时,并没有找到我正在寻找的东西。 说我有一个看起来像这样的课: public class MyClass { public void myMethod(boolean shouldCallOtherMethod) { if(shouldCallOtherMethod) { otherMethod(); } } public void otherMethod() { System.out.println(“Called”); } } 我如何制作这样的作品呢? @Test public void shouldCallMethod() { MyClass myClass = new MyClass(); myClass.myMethod(true) // verify myClass.otherMethod method was called }

PowerMock + Emma – 私有静态方法和其他方法的代码覆盖率显示为0%

我参考了PowerMock: 使用PowerMockito模拟私有方法并在此处应用相同的逻辑。 另外,我在eclipse / STS中安装了EMMA(开源工具),但是当我运行代码时,我看到零%的代码覆盖率。 为什么? public class MyClient { public void publicApi() { System.out.println(“In publicApi”); int result = 0; try { result = privateApi(“hello”, 1); } catch (Exception e) { //Assert.fail(); } System.out.println(“result : “+result); if (result == 20) { throw new RuntimeException(“boom”); } } private static int privateApi(String whatever, int num) throws Exception […]

如何在Java中模拟静态方法?

我有一个类FileGenerator ,我正在为generateFile()方法编写一个测试,它应该执行以下操作: 1)它应该在BlockAbstractFactory上调用静态方法getBlockImpl(FileTypeEnum) 2)它应该从子类方法getBlocks()填充变量blockList 3)它应该从最终帮助器类FileHelper调用一个静态方法createFile传递一个String参数 4)它应该调用BlockController中每个BlockController的run方法 到目前为止,我有这个空方法: public class FileGenerator { // private fields with Getters and Setters public void generateBlocks() { } } 我正在使用JUnit,Mockito来模拟对象,我尝试使用PowerMockito来模拟静态和最终类(Mockito不这样做)。 我的问题是:我的第一个测试(来自BlockAbstractFactory调用方法getBlockList() )正在传递,即使generateBlocks()没有实现。 我已经在BlockAbstractFactory实现了静态方法(到目前为止返回null),以避免Eclipse语法错误。 如何测试在fileGerator.generateBlocks()是否调用静态方法? 到目前为止,这是我的测试类: @RunWith(PowerMockRunner.class) public class testFileGenerator { FileGenerator fileGenerator = new FileGenerator(); @Test public void shouldCallGetBlockList() { fileGenerator.setFileType(FileTypeEnum.SPED_FISCAL); fileGenerator.generateBlocks(); PowerMockito.mockStatic(BlockAbstractFactory.class); PowerMockito.verifyStatic(); BlockAbstractFactory.getBlockImpl(fileGenerator.getFileType()); } }

如何使用PowerMock和Mockito模拟枚举类的实例?

我试着按照这个非常相似的问题的答案提供的例子,但它对我不起作用。 我收到以下错误消息: java.lang.IllegalArgumentException: Cannot subclass final class class com.myproject.test.support.ExampleEnumerable at org.mockito.cglib.proxy.Enhancer.generateClass(Enhancer.java:447) at org.mockito.cglib.core.DefaultGeneratorStrategy.generate(DefaultGeneratorStrategy.java:25) at org.mockito.cglib.core.AbstractClassGenerator.create(AbstractClassGenerator.java:217) at org.mockito.cglib.proxy.Enhancer.createHelper(Enhancer.java:378) at org.mockito.cglib.proxy.Enhancer.createClass(Enhancer.java:318) at org.powermock.api.mockito.repackaged.ClassImposterizer.createProxyClass(ClassImposterizer.java:123) at org.powermock.api.mockito.repackaged.ClassImposterizer.imposterise(ClassImposterizer.java:57) at org.powermock.api.mockito.internal.mockcreation.MockCreator.createMethodInvocationControl(MockCreator.java:110) at org.powermock.api.mockito.internal.mockcreation.MockCreator.mock(MockCreator.java:58) at org.powermock.api.mockito.PowerMockito.mock(PowerMockito.java:143) 我需要一个enum class的简单模拟实例。 我不需要模拟它的任何方法。 这是我想要模拟的类: public enum ExampleEnumerable implements IEnumerable { EXAMPLE_ENUM_1(“Test Enum 1”), EXAMPLE_ENUM_2(“Test Enum 2”); final String alias; ExampleEnumerable(final String alias) { this.alias = alias; […]

PowerMock和Java 8问题:InterfaceMethodrefInfo无法强制转换为MethodrefInfo

我在尝试使用PowerMock和Mockito执行unit testing时遇到了问题。 我需要PowerMockito来模拟静态方法。 这些是我使用的版本: PowerMock 1.6.2 Mockito 1.10.19 JUnit 4.12 Java 8 当我添加注释@PrepareForTest(Graph.class)时,我收到以下错误: java.lang.IllegalStateException: Failed to transform class with name name.of.my.package.GraphUtil. Reason: javassist.bytecode.InterfaceMethodrefInfo cannot be cast to javassist.bytecode.MethodrefInfo 我在官方的PowerMock Google页面上看到这与javassist有关。 但我有点失落,我不知道如何解决它。 为了以防万一,我也尝试下载最新的Powermock SNAPSHOT(1.6.3-SNAPSHOT),但也没有用。 请问有人帮帮我吗? 提前致谢

使用返回整数列表的power mock测试私有方法

我有一个私有方法,它取一个整数值列表返回一个整数值列表。 我怎样才能使用power mock来测试它。 我是powermock的新手。我可以用简单的模拟进行测试..? 怎么样..

在测试类中模拟私有静态final变量

我在要测试的类中有一些私有的静态final字段。 如下 public class ClassToTest{ …. private static final Myclass myclass = MyClassFactory.getMyClass(type.firstType); …. } 该类型是MyClassFactory中的枚举。 那个工厂做的是根据传递和返回的类型初始化对象。 我的问题是powermock是否支持这一点,如果是的话,该如何做到这一点。

PowerMock PrepareForTest注释导致AmazonSQSClient构造函数出现问题

我在使用PrepareForTest注释和创建AmazonSQSClient的新实例时遇到了一些麻烦。 我正在写一个Jenkins插件,不幸的是需要模拟FormValidation静态类,以确保在我的插件的字段validation时产生警告和错误消息。 但是在创建AmazonSQSClient的实例时,我得到一个org.apache.http.conn.ssl.SSLInitializationException 我把它抽象成一个非常简单的例子,这是我的测试文件: package com.test; import com.amazonaws.services.sqs.AmazonSQS; import com.amazonaws.services.sqs.AmazonSQSClient; import org.junit.Test; import org.junit.runner.RunWith; import org.powermock.core.classloader.annotations.PrepareForTest; import org.powermock.modules.junit4.PowerMockRunner; @RunWith(PowerMockRunner.class) public class SQSTest { // This is for mimicking the mocking of FormValidation private static class Foo {} @Test @PrepareForTest(Foo.class) public void buildTest() { AmazonSQS sqs = new AmazonSQSClient(); } } 在运行此代码时,我收到以下错误: org.apache.http.conn.ssl.SSLInitializationException: Failure initializing default […]

对于带参数的方法,Powermockito doNothing

我用Java开发了一个应用程序,我正在尝试使用Powermockito创建unit testing(我应该补充一点,我是unit testing的新手)。 我有一个名为Resource的类,它有一个名为readResources的静态方法: public static void readResources(ResourcesElement resourcesElement); ResourcesElement也由我编码。 在测试中,我想创建自己的资源,所以我希望上面的方法什么都不做。 我尝试使用此代码: PowerMockito.spy(Resource.class); PowerMockito.doNothing().when(Resource.class, “readResources”, Matchers.any(ResourcesElement.class)); unit testing抛出exception: org.mockito.exceptions.misusing.UnfinishedStubbingException:此处检测到未完成的存根: – > at org.powermock.api.mockito.internal.PowerMockitoCore.doAnswer(PowerMockitoCore.java:36) Powermockito还建议我应该使用thenReturn或者之后使用Thhrow,但似乎’when’方法在doNothing之后调用时返回void(这是合乎逻辑的)。 如果我尝试: PowerMockito.when(Resource.class, “readResources”, Matchers.any(ResourcesElement.class))….. 什么时候什么都不是一个选择。 我设法使用方法的2个参数版本创建没有参数的方法,什么都不做。 例如: PowerMockito.doNothing().when(Moduler.class, “startProcessing”); 这工作(startProcessing不接受任何参数)。 但是,我怎样才能制定出与Powermockito无关的方法呢?

使用@RunWith Annotation和powerMock时出现问题

最初我在junits中只使用Mockito所以我在@RunWith注释中使用了SpringJUnit4ClassRunner.class,即 @RunWith(SpringJUnit4ClassRunner.class) 由于春季dependency injection工作正常,并正在通过 @Autowired Someservice someservice ; 但现在,我还在其中集成了PowerMock。 所以根据doc ,我已经用@RunWith注释替换了提到的类 @RunWith(PowerMockRunner.class) 但是现在,有些服务似乎是空的。 有没有办法在@RunWith注释中同时使用SpringJUnit4ClassRunner.class和PowerMockRunner.class