2015-09-07 17 views
-2

SimpleA.java類是目標類。我嘲笑SimpleB.java類的GetList方法,如果相加法非靜態它工作正常,但它顯示靜態add方法問題如果add方法是靜態的,則不會得到答案,否則非靜態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"); 
      } 




     } 

回答

0

我已經看到在過去的這種行爲。解決方法是首先設置靜態方法的默認答案,然後填寫以下行爲:

@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 
}