Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.8k views
in Technique[技术] by (71.8m points)

java - Mock Protected Parent Method When the Parent is in a Different Package

I need to mock a protected method in the parent class of my class under test but the parent class is in a different package so my test class cannot access that method so I cannot mock it. There's gotta be a solution for this issue without refactoring

I need to use Powermock and Mockito. Here's the JARs

  • mockito-all 1.10.8
  • powermock-core 1.6.1
  • powermock-module-junit4 1.6.1
  • powermock-api-mockito 1.6.1
  • junit 4.12

This is legacy code so I cannot refactor, but here's the simplified code.

Parent.java

package parent;

public class Parent {

    // Want to mock this protected parent method from different package
    protected String foo() {

        String someValue = null;

        // Logic setting someValue

        return someValue;
    }
}

Child.java

package child;

import parent.Parent;

public class Child extends Parent {

    String fooString = null;

    public String boo() {

        this.fooString = this.foo();

        String booString = null;

        // Logic setting booString

        return booString;
    }
}

ChildTest.java

package child;

import static org.mockito.Mockito.spy;

import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;

import parent.Parent;

@RunWith(PowerMockRunner.class)
@PrepareForTest({ Parent.class, Child.class })
public class ChildTest {

    // Class Under Test
    Child cut;

    @Before
    public void setUp() throws Exception {

        // Partial mock to mock methods in parent class
        cut = spy(new Child());

    }

    @Test
    public void testBoo() {

        // TODO: Need to mock cut.foo() but can't figure out how.

        // Following gives me this error: The method foo() from the type Parent is not visible
       Mockito.when(((Parent)cut).foo()).thenReturn("mockValue");

        // Test
        cut.boo();

        // Validations
        Assert.assertEquals(cut.fooString, "mockValue");

    }
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Just create a new class for testing extending the class you want to test, and override your method there.

That would look something like that:

public class ChildForTest extends Child{
     @Override
     protected String foo() {
         //mock logic here
    }
}

Edit: If you want to avoid new class definition you can use anonymous class

@Before
public void setUp() throws Exception {

    // Partial mock to mock methods in parent class
    cut = new Child(){
        @Override
        protected String foo(){
            //mock logic here
            return "";
        }
    };
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...