【问题标题】:Not getting the answer if add method is static otherwise non-static add method is working如果 add 方法是静态的,则没有得到答案,否则非静态 add 方法正在工作
【发布时间】:2015-09-07 06:38:40
【问题描述】:

SimpleA.java 类是目标类。如果 add 方法是非静态的,我必须模拟 SimpleB.java 类的 getList 方法,它工作正常,但它显示静态 add 方法有问题

SimpleA.java

     import java.util.ArrayList;
        public class SimpleA {
                private   static  SimpleB obj1  ;

            public  static int add(){
                ArrayList<Integer> arr1 =  obj1.getList1();
                ArrayList<Integer> arr2 =  obj1.getList2();


                int res=0;

                for(Integer val : arr1){

                    res+=val;
                }
                for(Integer val1 : arr2){
                    res+=val1;
                }

                return res;


            }
        }

        SimpleB.java

        import java.util.ArrayList;
        import java.util.Iterator;


        public class SimpleB {

            public   ArrayList<Integer> getList1(){
                System.out.println("inside class SimpleB and getlist1");
                ArrayList<Integer> lst1 = new ArrayList<Integer>();
                lst1.add(10);
                lst1.add(20);
                return lst1;
            }
            public   ArrayList<Integer> getList2(){
                System.out.println("inside class SimpleB and getlist2");
                ArrayList<Integer>  lst2 = new ArrayList<Integer>();
                lst2.add(30);
                lst2.add(40);
                return lst2;
            }
        }

SimpleATest.java

这是测试类,我正在测试 SimpleA.java 类

        import java.lang.reflect.Field;
        import java.util.ArrayList;

        import org.easymock.EasyMock;
        import org.junit.After;
        import org.junit.Before;
        import org.junit.Test;
        import org.junit.runner.RunWith;
        import org.powermock.api.easymock.PowerMock;
        import org.powermock.api.mockito.PowerMockito;
        import org.powermock.core.classloader.annotations.PrepareForTest;
        import org.powermock.modules.junit4.PowerMockRunner;
        import org.powermock.reflect.Whitebox;



        @RunWith(PowerMockRunner.class)
        @PrepareForTest({SimpleA.class})

        public class SimpleATest {
                  SimpleA simpleA1;
                  SimpleB mockSample;




            @Before
            public  void setUp(){
                ArrayList<Integer> lst = new ArrayList<Integer>();
                lst.add(30);
                lst.add(40);
                lst.add(100);

                simpleA1 = new SimpleA();

                mockSample = PowerMock.createMock(SimpleB.class);
                PowerMock.mockStatic(SimpleA.class);
                EasyMock.expect(mockSample.getList1()).andReturn(lst);
                EasyMock.expect(mockSample.getList2()).andReturn(lst);
                PowerMock.replay(mockSample);

                Whitebox.setInternalState(simpleA1,mockSample);




                System.out.println("Init method is invoked");
            }

            @Test
            public void testAdd(){
                int res = simpleA1.add();
                System.out.println("res = "+res);

            }

            @After
            public void destroy(){
                System.out.println("Destroy method is invoked");
            }




        }

【问题讨论】:

    标签: powermock


    【解决方案1】:

    我过去曾见过这种行为。解决方法是首先在静态方法上设置默认答案,然后填写行为:

    @RunWith(PowerMockRunner.class)
    @PrepareForTest(MyClassWithStaticMethods.class)
    public testClass{
    
        @Test
        public void testSomething(){
            // this prepares the class; all static methods get a default return value (in this case type Object is returned and the default returned object is null)
            PowerMockito.mockStatic(MyClassWithStaticMethods.class, new Answer<Object>() 
            {
                @Override
                public Object answer(InvocationOnMock invocation) throws Throwable {
                    return null;
                }
            });
    
            //Now the class is prepared, we can just change the behaviour of the static method in the way we would like.
            String in = "example input string";
            String out = "example output string";
    
            // after preparing the class, you can override a specific static method. For this example we use the matchers, could also have used Matchers.any(String.class) or any other matcher combination as long as it maps on the method.
            PowerMockito.when(MyClassWithStaticMethods.staticMethod(Matchers.eq(in))).thenReturn(out);
        }
        //..body
    }
    

    【讨论】:

      猜你喜欢
      • 2016-08-07
      • 1970-01-01
      • 1970-01-01
      • 2013-04-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-01-17
      • 1970-01-01
      相关资源
      最近更新 更多