2017-02-27 65 views
1

我嘲笑以下方法:文件/路徑/文件實用程序 - 模擬幾個靜態類在一個測試

public static void cleanAndCreateDirectories(@NonNull final Path path) throws IOException { 
     // If download directory exists(should not be symlinks, clear the contents. 
     System.out.println(path); 
     if (Files.exists(path, LinkOption.NOFOLLOW_LINKS)) { 
      System.out.println("lol"); 
      FileUtils.cleanDirectory(path.toFile()); 
     } else { 
      // Eager recursive directory creation. If already exists then doesn't do anything. 
      Files.createDirectories(path); 
     } 
    } 

我試圖做這樣的,但它不工作:

@Test 
public void cleanAndCreateDirectoriesPathExistsHappyCase() throws IOException { 
    PowerMockito.mockStatic(Paths.class); 
    PowerMockito.when(Paths.get("/dir/file")).thenReturn(FileSystems.getDefault().getPath("/dir/file")); 
    PowerMockito.mockStatic(Files.class); 
    PowerMockito.when(Files.exists(Paths.get("/dir/file"), LinkOption.NOFOLLOW_LINKS)).thenReturn(true); 
    File file = new File("/dir/file"); 
    Mockito.when(Paths.get("/dir/file").toFile()).thenReturn(file); 
    PowerMockito.mockStatic(FileUtils.class); 
    // PowerMockito.when(FileUtils.cleanDirectory(path.toFile()); 
    // FileUtils.cleanDirectory(path.toFile()); 
    // PowerMockito.verifyStatic(Mockito.times(1)); 
    // FileUtils.cleanDirectory(path.toFile()); 
    ArsDumpGeneratorUtil.cleanAndCreateDirectories(Paths.get("/dir/file")); 
} 

我收到以下異常消息:

File cannot be returned by get() 
    [junit] get() should return Path 
    [junit] *** 
    [junit] If you're unsure why you're getting above error read on. 
    [junit] Due to the nature of the syntax above problem might occur because: 
    [junit] 1. This exception *might* occur in wrongly written multi-threaded tests. 
    [junit] Please refer to Mockito FAQ on limitations of concurrency testing. 
    [junit] 2. A spy is stubbed using when(spy.foo()).then() syntax. It is safer to stub spies - 
    [junit] - with doReturn|Throw() family of methods. More in javadocs for Mockito.spy() method. 
    [junit] 
    [junit] org.mockito.exceptions.misusing.WrongTypeOfReturnValue: 
    [junit] File cannot be returned by get() 
    [junit] get() should return Path 
    [junit] *** 
    [junit] If you're unsure why you're getting above error read on. 
    [junit] Due to the nature of the syntax above problem might occur because: 
    [junit] 1. This exception *might* occur in wrongly written multi-threaded tests. 
    [junit] Please refer to Mockito FAQ on limitations of concurrency testing. 
    [junit] 2. A spy is stubbed using when(spy.foo()).then() syntax. It is safer to stub spies - 
    [junit] - with doReturn|Throw() family of methods. More in javadocs for Mockito.spy() method. 
    [junit] 
    [junit]  at com.amazon.arsdumpgenerator.helpers.ArsDumpGeneratorUtilTest.cleanAndCreateDirectoriesPathExistsHappyCase(ArsDumpGeneratorUtilTest.java:62) 
    [junit]  at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$PowerMockJUnit44MethodRunner.runTestMethod(PowerMockJUnit44RunnerDelegateImpl.java:310) 
    [junit]  at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$PowerMockJUnit44MethodRunner.executeTest(PowerMockJUnit44RunnerDelegateImpl.java:294) 
    [junit]  at org.powermock.modules.junit4.internal.impl.PowerMockJUnit47RunnerDelegateImpl$PowerMockJUnit47MethodRunner.executeTestInSuper(PowerMockJUnit47RunnerDelegateImpl.java:127) 
    [junit]  at org.powermock.modules.junit4.internal.impl.PowerMockJUnit47RunnerDelegateImpl$PowerMockJUnit47MethodRunner.executeTest(PowerMockJUnit47RunnerDelegateImpl.java:82) 
    [junit]  at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$PowerMockJUnit44MethodRunner.runBeforesThenTestThenAfters(PowerMockJUnit44RunnerDelegateImpl.java:282) 
    [junit]  at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.invokeTestMethod(PowerMockJUnit44RunnerDelegateImpl.java:207) 
    [junit]  at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.runMethods(PowerMockJUnit44RunnerDelegateImpl.java:146) 
    [junit]  at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$1.run(PowerMockJUnit44RunnerDelegateImpl.java:120) 
    [junit]  at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.run(PowerMockJUnit44RunnerDelegateImpl.java:122) 
    [junit]  at org.powermock.modules.junit4.common.internal.impl.JUnit4TestSuiteChunkerImpl.run(JUnit4TestSuiteChunkerImpl.java:106) 
    [junit]  at org.powermock.modules.junit4.common.internal.impl.AbstractCommonPowerMockRunner.run(AbstractCommonPowerMockRunner.java:53) 
    [junit]  at org.powermock.modules.junit4.PowerMockRunner.run(PowerMockRunner.java:59) 

我做錯了什麼?請告訴我這方面的最佳做法。 進一步查詢:

  1. 模擬非空靜態類的靜態方法。
  2. 模擬靜態類的靜態方法。
  3. 模擬靜態類的非無效非靜態方法。
  4. 模擬void靜態類的非靜態方法。
  5. 如何檢查以上情況下的Mockito.times(N)。

如何做所有這些在一個單一的測試:

  1. 如果有問題的類是包含所有類型的方法只是一個單一的X類。

回答

1

解決如下:

@Test 
public void cleanAndCreateDirectoriesPathExistsHappyCase() throws IOException { 
    PowerMockito.mockStatic(Files.class); 
    PowerMockito.when(Files.exists(Paths.get("/dir/Exist"), LinkOption.NOFOLLOW_LINKS)).thenReturn(true); 
    // Mock FileUtils. 
    PowerMockito.mockStatic(FileUtils.class); 
    PowerMockito.doNothing().when(FileUtils.class); 
    // Call internal-static method. 
    FileUtils.cleanDirectory(Matchers.any()); 
    // Call target method. 
    ArsDumpGeneratorUtil.cleanAndCreateDirectories(Paths.get("/dir/Exist")); 
    // Check internal-static method call. 
    PowerMockito.verifyStatic(Mockito.times(1)); 
    FileUtils.cleanDirectory(Matchers.any()); 
} 

@Test 
public void cleanAndCreateDirectoriesPathNotExistsHappyCase() throws IOException { 
    PowerMockito.mockStatic(Files.class); 
    PowerMockito.when(Files.exists(Paths.get("/dir/NotExist"), LinkOption.NOFOLLOW_LINKS)).thenReturn(false); 
    PowerMockito.when(Files.createDirectories(Paths.get("/dir/NotExist"))).thenReturn(Paths.get("/dir/NotExist")); 
    // Call target method. 
    ArsDumpGeneratorUtil.cleanAndCreateDirectories(Paths.get("/dir/NotExist")); 
    // Check internal-static method call. 
    PowerMockito.verifyStatic(Mockito.times(1)); 
    Files.createDirectories(Matchers.any()); 
}