java.nio.file.FileSystemException:无法访问该文件,因为它正被另一个进程

时间:2016-08-11 07:04:55

标签: java unit-testing junit cassandra powermock

我正在使用Junit编写单元测试类,我也使用power-mocking概念来模拟在方法中创建的对象。

测试类:

@PowerMockIgnore("javax.management.*")
@RunWith(PowerMockRunner.class)
@PrepareForTest(Client.class)
public class ClientTest {
private static final Logger logger = LoggerFactory
        .getLogger(ClientTest.class);

private static Client client;

private static DetailsDao detailsDao;
public static Session session;
private static IDevicesDao devicesDao;
private static IUserDao userDao;
private static IDao dao;
private static IValidator validator;



@BeforeClass
public static void setup() throws Exception {

    logger.info("*************** Testing ClientTest Started *******************");

    session = Connection.get(); // Here getting error

    detailsDao = mock(DetailsDao.class);
    devicesDao = mock(IDevicesDao.class);
    userDao = mock(UserDao.class);
    validator = mock(IValidator.class);
    dao= mock(IDao.class);

    client = new Client(detailsDao, session, devicesDao, userDao, dao, validator);
}

如果我退出@RunWith(PowerMockRunner.class)那么其工作正常......但我必须仅使用PowerMockRunner运行此测试。

例外:

[10:45:50] sumanth nama: Tests run: 1, Failures: 0, Errors: 1, Skipped: 0,      Time elapsed: 1.234 sec <<< FAILURE!
com.service.clientTest  Time elapsed:       1.234 sec  <<< ERROR!
FSWriteError in target\embeddedCassandra\commitlog\CommitLog-4-1470892233532.log
at    org.apache.cassandra.io.util.FileUtils.deleteWithConfirm(FileUtils.java:135)
at org.apache.cassandra.io.util.FileUtils.deleteRecursive(FileUtils.java:381)
at org.apache.cassandra.io.util.FileUtils.deleteRecursive(FileUtils.java:377)
at org.apache.cassandra.io.util.FileUtils.deleteRecursive(FileUtils.java:377)
at  org.cassandraunit.utils.EmbeddedCassandraServerHelper.rmdir(EmbeddedCassandraSer verHelper.java:266)
at org.cassandraunit.utils.EmbeddedCassandraServerHelper.startEmbeddedCassandra(EmbeddedCassandraServerHelper.java:81)
 at org.cassandraunit.utils.EmbeddedCassandraServerHelper.startEmbeddedCassandra(EmbeddedCassandraServerHelper.java:64)
at org.cassandraunit.utils.EmbeddedCassandraServerHelper.startEmbeddedCassandra(EmbeddedCassandraServerHelper.java:56)
at com.charter.aesd.crossmso.wifi.dao.Connection.get(Connection.java:26)
at com.service.ClientTest.setup(ClientTest.java:76)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at org.junit.internal.runners.ClassRoadie.runBefores(ClassRoadie.java:57)
at org.junit.internal.runners.ClassRoadie.runProtected(ClassRoadie.java:44)
at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.run(PowerMockJUnit44RunnerDelegateImpl.java:122)
at org.powermock.modules.junit4.common.internal.impl.JUnit4TestSuiteChunkerImpl.run(JUnit4TestSuiteChunkerImpl.java:106)
at org.powermock.modules.junit4.common.internal.impl.AbstractCommonPowerMockRunner.run(AbstractCommonPowerMockRunner.java:53)
at org.powermock.modules.junit4.PowerMockRunner.run(PowerMockRunner.java:59)
at o rg.apache.maven.surefire.junit4.JUnit4Provider.execute(JUnit4Provider.java:252)
at org.apache.maven.surefire.junit4.JUnit4Provider.executeTestSet(JUnit4Provider.java:141)
at org.apache.maven.surefire.junit4.JUnit4Provider.invoke(JUnit4Provider.java:112)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at org.apache.maven.surefire.util.ReflectionUtils.invokeMethodWithArray(ReflectionUtils.java:189)
at org.apache.maven.surefire.booter.ProviderFactory$ProviderProxy.invoke(ProviderFactory.java:165)
at org.apache.maven.surefire.booter.ProviderFactory.invokeProvider(ProviderFactory.java:85)
at org.apache.maven.surefire.booter.ForkedBooter.runSuitesInProcess(ForkedBooter.java:115)
at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:75)
Caused by: java.nio.file.FileSystemException:    target\embeddedCassandra\commitlog\CommitLog-4-1470892233532.log: The process    cannot access the file because it is being used by another process.

我没有得到......为什么它无法使用power mock runner创建cassandra会话....

1 个答案:

答案 0 :(得分:0)

也嘲笑会话,然后运行没有任何错误。

替换

session = Connection.get();

session = mock(Session.class);