2015-10-20 132 views
0

我想模擬一個內部調用私有方法的方法。這個私有方法使用classloader.getclass方法讀取一個文件並填充一個列表,該列表是該類的靜態變量。我嘗試了所有可能的方法來嘲笑這種私人方法,但似乎並不奏效。嘲笑一個私人方法

class TestLoad { 
    private static List <String> myList = new ArrayList <String>(); 
    private static final String filename = "/myfile.txt"; 

    public XYZ generateList(Abc abc) throws Exception { 
     populateList(); 
    } 

    private populateList() { 
     ClassLoader classLoader = TestLoad.class.getClassLoader(); 
     File listfile = new File(classLoader.getResource(
     filename).getFile()); 
     List <String> localList = new ArrayList <String>(); 

     try (Scanner scanner = new Scanner(listfile)) { 
      while (scanner.hasNextLine()) { 
       String text = scanner.nextLine(); 
       localList.add(text.trim()); 
      } 
      scanner.close(); 
     } catch (IOException e) {} 
     return localList; 
    } 
} 

我試圖模擬populateList()但控制進入該方法的所有時間。我幾乎嘗試了所有的選項在網上雙方及的Mockito但powermock它似乎並沒有work.My的Junit不會是如下

級開始有@RunWith(PowerMockRunner.class) @PrepareForTest(TestLoad.class ) ArrayList testList = new ArrayList(); testList.add(「00」); TestLoad實例= PowerMock.createPartialMock(TestLoad.class,「populateList」); PowerMock.expectPrivate(instance,「populateList」)andReturn(testList); PowerMock.replay(instance);

下面是日誌以供參考:

java.lang.IllegalStateException:無法用名com.xyz.TestLoad改造類。原因:java.io.IOException:無效的常量類型:17 at 17 at org.powermock.core.classloader.MockClassLoader.loadMockClass(MockClassLoader.java:266) at org.powermock.core.classloader.MockClassLoader.loadModifiedClass(MockClassLoader .java:180) at org.powermock.core.classloader.DeferSupportingClassLoader.loadClass(DeferSupportingClassLoader.java:68) at java.lang.ClassLoader.loadClass(ClassLoader.java:357) at java.lang.Class.forName0 (本機方法) 在java.lang.Class.forName(Class.java:348) 在org.powermock.modules.junit4.common.internal.impl.JUnit4TestSuiteChunkerImpl.createDelegatorFromClassloader(JUnit4TestSuiteChunkerImpl.java:145) 的組織。 powermock.modules.junit4.common.internal.impl.JUnit4TestSuiteChunkerImpl.createDelegat orFromClassloader(JUnit4TestSuiteChunkerImpl.java:40) 在org.powermock.tests.utils.impl.AbstractTestSuiteChunkerImpl.createTestDelegators(AbstractTestSuiteChunkerImpl.java:244) 在org.powermock.modules.junit4.common.internal.impl.JUnit4TestSuiteChunkerImpl。(JUnit4TestSuiteChunkerImpl的.java:61) 在org.powermock.modules.junit4.common.internal.impl.AbstractCommonPowerMockRunner(AbstractCommonPowerMockRunner.java:32) 在org.powermock.modules.junit4.PowerMockRunner(PowerMockRunner.java:34) 。在sun.reflect.NativeConstructorAccessorImpl.newInstance0(本機方法)

+1

後的測試代碼你都試過了。你在這裏發佈的代碼有很多編譯錯誤。 – kswaughs

+0

下面是代碼TestLoad實例= PowerMock \t \t \t \t .createPartialMock(TestLoad.class, \t \t \t \t \t \t 「populateList」); \t \t PowerMock.expectPrivate(例如, 「populateList」)andReturn( \t \t \t \t testList)。 \t \t PowerMock.replay(instance); –

回答

0

後我找到了很多研究解決方案。我們需要將以下添加爲依賴在pom.xml防止這種錯誤... **

<dependency> 
      <groupId>org.javassist</groupId> 
      <artifactId>javassist</artifactId> 
      <version>3.18.2-GA</version> 
     </dependency> 

**

0

你的測試代碼看起來像您使用PowerMock EasyMock的。 但您的文章也標有mockito & powermockito。

我給下面的解決方案使用powermockito。

主要的Java類:

public class TestLoad { 

private static List<String> myList = new ArrayList<String>(); 
private static final String filename = "/myfile.txt"; 

public List<String> generateList(Abc abc) throws Exception { 

    System.out.println("generateList method is called"); 
    return populateList(); 
} 

private List<String> populateList() { 
    System.out.println("populateList method is called"); 
    ClassLoader classLoader = TestLoad.class.getClassLoader(); 
    File listfile = new File(classLoader.getResource(filename).getFile()); 
    // Read listfile and build localList logic here 
    List<String> localList = new ArrayList<String>(); 

    return localList; 
} 
} 

測試Java類

import java.util.ArrayList; 
import java.util.List; 

import org.junit.Test; 
import org.junit.runner.RunWith; 
import org.mockito.Matchers; 
import org.mockito.Mockito; 
import org.powermock.api.mockito.PowerMockito; 
import org.powermock.core.classloader.annotations.PrepareForTest; 
import org.powermock.modules.junit4.PowerMockRunner; 

@PrepareForTest(TestLoad.class) 
@RunWith(PowerMockRunner.class) 
public class TestLoadTest { 

@Test 
public void testPrivateMethod() throws Exception{ 

    List<String> testList = new ArrayList<String>(); 
    testList.add("00"); 

    TestLoad mockInstance = PowerMockito.spy(new TestLoad()); 

    PowerMockito.doReturn(testList).when(mockInstance, "populateList"); 

    List<String> outputList = mockInstance.generateList(new Abc()); 

    System.out.println("test output:" + outputList); 

    PowerMockito.verifyPrivate(mockInstance, Mockito.times(1)).invoke("populateList"); 

} 

} 

控制檯輸出:

generateList method is called 
test output:[00] 
+0

我已經試過這種方法,它給了我在上面的帖子中提到的同樣的錯誤。這裏的問題是不同的。經過大量的研究,我找到了解決方案。我們需要增加以下內容作爲在pom.xml依賴... \t \t \t org.javassist \t Javassist是 \t 3.18.2-GA \t