简体   繁体   中英

Mockito test failing to initialize

This fails with InitializationError. Other tests in the same package run so I have done something silly in my code. Stacktrace reads "No tests found matching [[Exactmatch]]".

public class TestClassToTest {
    @Mock
    File mockOfAFile;

    @Test
    public void testAMethod(File mockOfAFile) {
        MockitoAnnotations.initMocks(this);
        given(fileMock.getName()).willReturn("test1");
        assertEquals("test1",ClassBeingTested.methodBeingTested(mockOfAFile));
    }
}

Have tried everything but am very new to Mockito. What silly thing am I doing here ?

Thanks

I found two things to fix:

  1. The @Test method should have no parameters
  2. You need another File instance, called fileMock .

So here is the updated code:

public class TestClassToTest {

    @Mock
    File mockOfAFile;

    @Mock
    File fileMock; // the new mock

    @Test
    public void testAMethod() { // no parameters
        MockitoAnnotations.initMocks(this);
        given(fileMock.getName()).willReturn("test1"); // here is the new mock used
        assertEquals("test1",ClassBeingTested.methodBeingTested(mockOfAFile));
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM