gpt4 book ai didi

java - 如果测试运行正常,为什么我会收到反射异常 NoSuchMethodException

转载 作者:行者123 更新时间:2023-12-02 12:16:40 24 4
gpt4 key购买 nike

我正在使用反射来模拟私有(private)方法(我不想讨论这是否有意义)。

有人知道为什么吗?我将在这里提供我的 testClass 源代码,它可能会有所帮助。我已经尝试了很多互联网帮助和方法来解决这个问题,但没有一个对我有用。

public class testProtexManagerProcessRequiredFile {

@Mock
ProtexManager PxManager;


@Before
public void inicializa() {
MockitoAnnotations.initMocks(this);
}

@Test
public void processRequiredFileTest() throws ClassNotFoundException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, NoSuchMethodException, SecurityException, InstantiationException {

Method method;
try {
method = ProtexManager.class.getDeclaredMethod("processRequiredFile", File.class);
method.setAccessible(true);

File FileExample = new File();
String NameExample = "Nome";
File outputs = new File();
outputs = (File) Mockito.when(method.invoke(PxManager, FileExample,NameExample)).thenReturn(FileExample);
assertNotNull(outputs);
assertEquals(outputs, method.invoke(PxManager, FileExample,NameExample));
} catch (NoSuchMethodException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("Teste Concluido.");
}
}

这就是方法代码:

 private File processRequiredFile(File file, String name) {
if (!file.exists()) {
this.message = name + " file not found at location: " + file;
this.msgResponse.addMsgList(MsgCode.FAILURE, MsgLevel.ERROR, this.message, StringUtils.EMPTY);
}
return file;
}

感谢大家帮助我解决疑问。

最佳答案

回答你的问题,

因为您捕获了NoSuchMethodException。要获得测试失败,您必须在测试执行期间以某种方式获得一些异常或错误

<小时/>

为了跟进评论,以下是测试此方法的方法:

// let's assume there are getter for this.message / this.msgResponse 
// and this method is in the class foo.bar.Foobar
protected File processRequiredFile(File file, String name) {
if (!file.exists()) {
this.message = name + " file not found at location: " + file;
this.msgResponse.addMsgList(MsgCode.FAILURE, MsgLevel.ERROR, this.message, StringUtils.EMPTY);
}
return file;
}

在测试类中foo.bar.FoobarTest:

@Mock
private File file;

private Foobar foobar = new Foobar();

@Test
public void testWithNonExistingFile() {
Mockito.when(this.file.exists()).thenReturn(false); // this is to illustrate, you could also use some non existent file: new File("/does-not-exists.foo")
File result = this.foobar.processRequiredFile(this.file, "some name");
assertThat(result).isEqualTo(this.file);
assertThat(foobar.getMsgResponse()).isNotEmpty(); // TODO: better assertion
assertThat(foobar.getMessage()).isEqualTo( "some name file not found at location: " + this.file);
}

@Test
public void testWithExistingFile() {
Mockito.when(this.file.exists()).thenReturn(true);
File result = this.foobar.processRequiredFile(this.file, "some name");
assertThat(result).isEqualTo(this.file);
assertThat(foobar.getMsgResponse()).isEmpty();
assertThat(foobar.getMessage()).isNull();
}

被测试的类(即Foobar)经过真正的测试,它使用它的真实实例并调用它的方法。模拟用于替换我们没有的东西(这里是一个文件来说明,但通常是更复杂的东西)

关于java - 如果测试运行正常,为什么我会收到反射异常 NoSuchMethodException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46117252/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com