Java Mockito测试无法初始化
此操作失败,出现初始化错误。同一个包中的其他测试运行,因此我在代码中做了一些愚蠢的事情。Stacktrace读取“未找到匹配[[Exactmatch]]的测试” 我什么都试过了,但对莫基托来说我是个新手。我在这里干什么蠢事Java Mockito测试无法初始化,java,eclipse,junit,mocking,mockito,Java,Eclipse,Junit,Mocking,Mockito,此操作失败,出现初始化错误。同一个包中的其他测试运行,因此我在代码中做了一些愚蠢的事情。Stacktrace读取“未找到匹配[[Exactmatch]]的测试” 我什么都试过了,但对莫基托来说我是个新手。我在这里干什么蠢事 谢谢我发现有两件事需要解决: @Test方法应该没有参数 您需要另一个名为fileMock的文件实例 下面是更新后的代码: public class TestClassToTest { @Mock File mockOfAFile; @Mock
谢谢我发现有两件事需要解决:
@Test
方法应该没有参数fileMock
的文件
实例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));
}
}
你能试着从你的测试方法签名中删除
File-mockOfAFile
,并且不保留任何参数吗?另外,你能试着用@UnitTest
注释你的测试类吗?你能告诉我们你是如何运行它们并附加实际的堆栈跟踪吗?
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));
}
}