1. <tfoot id='dmKmv'></tfoot>
  2. <i id='dmKmv'><tr id='dmKmv'><dt id='dmKmv'><q id='dmKmv'><span id='dmKmv'><b id='dmKmv'><form id='dmKmv'><ins id='dmKmv'></ins><ul id='dmKmv'></ul><sub id='dmKmv'></sub></form><legend id='dmKmv'></legend><bdo id='dmKmv'><pre id='dmKmv'><center id='dmKmv'></center></pre></bdo></b><th id='dmKmv'></th></span></q></dt></tr></i><div id='dmKmv'><tfoot id='dmKmv'></tfoot><dl id='dmKmv'><fieldset id='dmKmv'></fieldset></dl></div>
    <legend id='dmKmv'><style id='dmKmv'><dir id='dmKmv'><q id='dmKmv'></q></dir></style></legend>

      <small id='dmKmv'></small><noframes id='dmKmv'>

        <bdo id='dmKmv'></bdo><ul id='dmKmv'></ul>

      当父级在不同的包中时模拟受保护的父级方法

      时间:2023-09-25

      <tfoot id='vI5R0'></tfoot>
          <legend id='vI5R0'><style id='vI5R0'><dir id='vI5R0'><q id='vI5R0'></q></dir></style></legend>

              • <bdo id='vI5R0'></bdo><ul id='vI5R0'></ul>

              • <i id='vI5R0'><tr id='vI5R0'><dt id='vI5R0'><q id='vI5R0'><span id='vI5R0'><b id='vI5R0'><form id='vI5R0'><ins id='vI5R0'></ins><ul id='vI5R0'></ul><sub id='vI5R0'></sub></form><legend id='vI5R0'></legend><bdo id='vI5R0'><pre id='vI5R0'><center id='vI5R0'></center></pre></bdo></b><th id='vI5R0'></th></span></q></dt></tr></i><div id='vI5R0'><tfoot id='vI5R0'></tfoot><dl id='vI5R0'><fieldset id='vI5R0'></fieldset></dl></div>

                <small id='vI5R0'></small><noframes id='vI5R0'>

                  <tbody id='vI5R0'></tbody>
              • 本文介绍了当父级在不同的包中时模拟受保护的父级方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                问题描述

                我需要在我的测试类的父类中模拟一个受保护的方法,但父类位于不同的包中,因此我的测试类无法访问该方法,因此我无法模拟它.这个问题必须有一个无需重构的解决方案

                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

                我需要使用 Powermock 和 Mockito.这是 JAR

                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");
                
                    }
                }
                

                推荐答案

                你可以使用 PowerMock 来模拟没有公共方法.

                You can use PowerMock to mocking no public method.

                @RunWith(PowerMockRunner.class)
                public class ChildTest {
                
                    @Test
                    public void testBoo() throws Exception {
                        //given
                        Child child = PowerMockito.spy(new Child());
                        PowerMockito.when(child, "foo").thenReturn("mockValue");
                
                        //when
                        String boo = child.boo();
                
                        //then
                        Assert.assertEquals("boo+mockValue", boo);
                    }
                }
                

                这篇关于当父级在不同的包中时模拟受保护的父级方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                上一篇:编写 JUnit 测试用例请求调度程序时出错 下一篇:如何使用 MockRestServiceServer 模拟 RestTemplate?

                相关文章

                最新文章

                <i id='oSmwc'><tr id='oSmwc'><dt id='oSmwc'><q id='oSmwc'><span id='oSmwc'><b id='oSmwc'><form id='oSmwc'><ins id='oSmwc'></ins><ul id='oSmwc'></ul><sub id='oSmwc'></sub></form><legend id='oSmwc'></legend><bdo id='oSmwc'><pre id='oSmwc'><center id='oSmwc'></center></pre></bdo></b><th id='oSmwc'></th></span></q></dt></tr></i><div id='oSmwc'><tfoot id='oSmwc'></tfoot><dl id='oSmwc'><fieldset id='oSmwc'></fieldset></dl></div>
                <tfoot id='oSmwc'></tfoot>
              • <small id='oSmwc'></small><noframes id='oSmwc'>

                  <legend id='oSmwc'><style id='oSmwc'><dir id='oSmwc'><q id='oSmwc'></q></dir></style></legend>
                    • <bdo id='oSmwc'></bdo><ul id='oSmwc'></ul>