English 中文(简体)
电力管理中继承的受保护方法
原标题:TooManyMethodsFoundException for inherited protected methods in PowerMock

重新提出步骤,

  1. Create a class with a protected method and override the same in subclass.
  2. Create a test for the subclass and try to suppress the method call throws TooManyMethodsFoundException.

2. 试验接口。

package com.test.powermock;

/**
 * Created by dineshkumar on 06/05/16.
 */
public interface TestInterface {
    public int testMethod() throws Exception;
}

摘要

package com.test.powermock;

/**
 * Created by dineshkumar on 06/05/16.
 */
public abstract class AbstractTest implements TestInterface {

public int testMethod() throws Exception {
        return 0;
    }

    protected void voidMethodWithParams(String a) throws Exception{

    }
}

ImplClass Test.java

package com.test.powermock;

/**
 * Created by dineshkumar on 06/05/16.
 */
public class ImplClassTest extends AbstractTest {


    @Override
    public int testMethod() throws Exception {
        voidMethodWithParams("a");
        return 2;
    }

    @Override
    protected void voidMethodWithParams(String a) throws Exception{
        System.out.println("dd");
    }
}

ImplClass Test.java

package com.test.powermock;

import org.powermock.api.mockito.PowerMockito;
import static org.powermock.api.support.membermodification.MemberMatcher.method;
import static org.powermock.api.support.membermodification.MemberModifier.suppress;
import static org.testng.Assert.*;

/**
 * Created by dineshkumar on 06/05/16.
 */
public class ImplClassTestTest {

    @org.testng.annotations.Test
    public void testTestMethod() throws Exception {
        ImplClassTest implClassTestSpy = PowerMockito.spy(new ImplClassTest());
        suppress(method(ImplClassTest.class, "voidMethodWithParams", String.class));
        int res = implClassTestSpy.testMethod();
        assertEquals(res, 2);
    }
}

https://groups.google.com/forum/#

还研究了以下法典:

package com.test.powermock;

import org.powermock.api.mockito.PowerMockito;

import static org.powermock.api.support.membermodification.MemberMatcher.method;
import static org.powermock.api.support.membermodification.MemberModifier.suppress;
import static org.testng.Assert.*;

/**
 * Created by dineshkumar on 06/05/16.
 */
public class ImplClassTestTest {

    @org.testng.annotations.Test
    public void testTestMethod() throws Exception {
        ImplClassTest implClassTestSpy = PowerMockito.spy(new ImplClassTest());
        suppress(method(AbstractTest.class, "voidMethodWithParams", String.class));
        suppress(method(ImplClassTest.class, "voidMethodWithParams", String.class));
        int res = implClassTestSpy.testMethod();
        assertEquals(res, 2);
    }
}

我是否可以压制这些方法?

问题回答

问题:

不能正确获得<代码>@ 以上受保护的方法: 避免MethodWithParams,因为问题代码找到了两类方法: 标准/编码

<>Problem:发现两种方法! Powermock确实知道镇压哪一种。

    // encounter TooManyMethodsFoundException !!!
    suppress(method(AbstractTest.class, "voidMethodWithParams", String.class));

www.un.org/Depts/DGACM/index_spanish.htm 这个问题无疑是使用java原始途径foo.class.getMethod()(公用)bar.class.getDeClaredMethod((保护及私人)

// "voidMethodWithParams" is protected method use getDeclaredMethod()
 suppress(ImplClassTest.class.getDeclaredMethod("voidMethodWithParams", String.class))
// "publicMethod" use getMethod()
 suppress(ContainPublicMethodClass.class.getMethod("publicMethod", foo.class))

<>PS>/strong>: 如果你想要在某一类别中压制所有方法,则使用Powermock scode>methodsDeclaredIn(

import static org.powermock.api.support.membermodification.MemberMatcher.methodsDeclaredIn;
suppress(methodsDeclaredIn(AbstractTest.class))

1. 更新您的模拟版本,使其符合相容的模拟核心模拟仪。





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签