Static layout not working

I have the following unit test example that tries to mock java.nio.file.Files, but this layout does not work, and the code tries to remove the path to the sample.

@Test
    public void testPostVisitDirectory() throws Exception {
        Path mockedPath = Paths.get("sample path");
        PowerMockito.mockStatic(Files.class);
        PowerMockito.doNothing().when(Files.class,
                PowerMockito.method(Files.class, "delete", Path.class));

        DeleteDirVisitor visitor = new DeleteDirVisitor(false);
        Assert.assertEquals("The was a problem visiting the file",
                FileVisitResult.CONTINUE,
                visitor.postVisitDirectory(mockedPath, null));
    }

Any idea what's wrong?

this is the content of the method visitor.postVisitDirectory

[...]
if (e == null) {
            Files.delete(dir);
            return FileVisitResult.CONTINUE;
        }
[...]

Thank,

+5
source share
3 answers

You added

@RunWith(PowerMockRunner.class)
@PrepareForTest(Files.class)

to your junit test class containing this method?

See powermock docs , Writing Tests.

EDIT:

Hmm, it looks like you're doing everything right. Here is what I run:

import java.io.IOException;
import java.nio.file.FileVisitResult;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.SimpleFileVisitor;

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

@RunWith(PowerMockRunner.class)
@PrepareForTest(Files.class)
public class TestVisitor {
  public class PrintingVisitor extends SimpleFileVisitor<Path> {
    @Override
    public FileVisitResult postVisitDirectory(final Path dir, final IOException exc) throws IOException {
      Files.delete(dir);
      return FileVisitResult.CONTINUE;
    }
  }

  @Test
  public void testPostVisitDirectory() throws Exception {
    final Path mockedPath = Paths.get("sample path");

    /* Mocking */
    PowerMockito.mockStatic(Files.class);
    PowerMockito.doNothing().when(Files.class, PowerMockito.method(Files.class, "delete", Path.class));
    /* End Mocking */

    final PrintingVisitor visitor = new PrintingVisitor();
    Assert.assertEquals("The was a problem visiting the file", FileVisitResult.CONTINUE, visitor.postVisitDirectory(mockedPath, null));
  }
}

If I comment on the section labeled Mocking, I get NoSuchFileException. If I leave it, the test will pass.

Perhaps a complete example appears that causes an error?

+4
source

powermock 1.5.1 Files, , / jdk1.7, . javassist , (3.18.0-GA),

"", . StringUtils.chop( "string" ); (commons-lang3), , mock.

, , Files, StringUtils.

, , , @PrepareForTest PowerMockito.mockStatic() .

. -, - .

. : , . PowerMock out (1.5.3), javassist (3.18.1-GA), , .

Files , @PrepareForTest, Files , . . , - Files.

:

public class MyTestClass {

    public void justToTestMocking(Path path) throws IOException {
        if (!Files.exists(path)) {
            throw new IllegalArgumentException("I know there is a deleteIfExists() but I am just testing mocking");
        }
        Files.delete(path);
    }
}

:

@RunWith(PowerMockRunner.class)
@PrepareForTest({Files.class, MyTestClass.class})
public class MyTestClassTest {

    @Before
    public void setUp() {
        mockStatic(Files.class);

    }        

    @Test
    public void justToTestMocking_WillDeletePath() throws IOException {
        Path path = mock(Path.class);
        MyTestClass test = new MyTestClass();

        when(Files.exists(path)).thenReturn(true);

        test.justToTestMocking(path);

        verifyStatic();
        Files.delete(path);
    }
}
+2

, , .

"prepareFor-Scope", .

You have to add classes to @PrepareForTestthat call static methods ... and in my case it wasn’t enough, because the code that accessed Files.deleteit was inside an anonymous class that could not be explicitly prepared.

I called an anonymous class and added it to @PrepareForTest, and everything worked

+1
source

All Articles