문제

I have code like

private static Path file1;  // reference file

@BeforeClass
public static void setUpBeforeClass()
{
    Path file1 = Files.createTempFile(testFolder, "test-", ".txt");
}

@Test
public void testIdentical()
{
    assertNotNull(file1); // fails
}

But file1 is null. I have been searching for answers to this problem, and while the issues are similar, no-one ever seams to explain clearly what exactly is going on, or how to do what I want to do: which is for file1 to not be null.

I am doing what https://github.com/junit-team/junit/wiki/Test-fixtures seems to indicate is correct, but I am not getting the results I expect.

도움이 되었습니까?

해결책

You are assigning to a local variable here:

Path file1 = Files.createTempFile(testFolder, "test-", ".txt");

do this instead:

file1 = Files.createTempFile(testFolder, "test-", ".txt");
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top