Java 如何模拟Jsoup post调用并使用常量响应进行回复
我试图[使用Cucumber]对以下代码库进行单元测试Java 如何模拟Jsoup post调用并使用常量响应进行回复,java,cucumber,jsoup,mockito,powermock,Java,Cucumber,Jsoup,Mockito,Powermock,我试图[使用Cucumber]对以下代码库进行单元测试 final Connection connection = Jsoup.connect(PERFORMACE_TESTING_POST_URL); connection .followRedirects(false) .method(Method.POST); final org.jsoup.Connection.Response response = connection.execute(); fina
final Connection connection = Jsoup.connect(PERFORMACE_TESTING_POST_URL);
connection
.followRedirects(false)
.method(Method.POST);
final org.jsoup.Connection.Response response = connection.execute();
final String ptJobLocation = response.header(HTTP_HEADER_LOCATION);
message = "Performance Testing Job Triggered. It is available at " + ptJobLocation;
我尝试了以下灵感来源的解决方案
然而,我不断得到以下错误
cucumber.runtime.CucumberException: Failed to instantiate class com.dummy.package.ClassUnderTst
at cucumber.runtime.java.DefaultJavaObjectFactory.cacheNewInstance(DefaultJavaObjectFactory.java:46)
at cucumber.runtime.java.DefaultJavaObjectFactory.getInstance(DefaultJavaObjectFactory.java:32)
at cucumber.runtime.java.JavaStepDefinition.execute(JavaStepDefinition.java:38)
....
....
Caused by: java.lang.reflect.InvocationTargetException
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
at cucumber.runtime.java.DefaultJavaObjectFactory.cacheNewInstance(DefaultJavaObjectFactory.java:40)
... 16 more
Caused by: org.mockito.exceptions.misusing.MissingMethodInvocationException:
when() requires an argument which has to be 'a method call on a mock'.
For example:
when(mock.getArticles()).thenReturn(articles);
Also, this error might show up because:
1. you stub either of: final/private/equals()/hashCode() methods.
Those methods *cannot* be stubbed/verified.
Mocking methods declared on non-public parent classes is not supported.
2. inside when() you don't call method on mock but on some other object.
at com.dummy.package.ClassUnderTst.<init>(ClassUnderTst.java:60)
... 21 more
cucumber.runtime.CucumberException:未能实例化类com.dummy.package.ClassUnderTst
位于cucumber.runtime.java.DefaultJavaObjectFactory.cacheNewInstance(DefaultJavaObjectFactory.java:46)
在cucumber.runtime.java.DefaultJavaObjectFactory.getInstance(DefaultJavaObjectFactory.java:32)中
在cucumber.runtime.java.JavaStepDefinition.execute(JavaStepDefinition.java:38)中
....
....
原因:java.lang.reflect.InvocationTargetException
位于sun.reflect.NativeConstructorAccessorImpl.newInstance0(本机方法)
位于sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
在sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
位于java.lang.reflect.Constructor.newInstance(Constructor.java:423)
位于cucumber.runtime.java.DefaultJavaObjectFactory.cacheNewInstance(DefaultJavaObjectFactory.java:40)
... 还有16个
原因:org.mockito.exceptions.misusing.MissingMethodInvocationException:
when()需要的参数必须是“模拟的方法调用”。
例如:
when(mock.getArticles())。然后返回(articles);
此外,出现此错误的原因可能是:
1.您可以存根:final/private/equals()/hashCode()方法。
那些方法*不能*被存根/验证。
不支持对非公共父类声明的模拟方法。
2.在when()中,您不在mock上调用方法,而是在其他对象上调用方法。
位于com.dummy.package.ClassUnderTst。(ClassUnderTst.java:60)
... 还有21个
任何有关这方面的帮助都将不胜感激
使用的LIB如下所示
问题是什么?我已经重新组织了这个问题。现在应该可以提供信息了。有什么问题吗?我已经重新组织了这个问题。它现在应该是信息性的。
cucumber.runtime.CucumberException: Failed to instantiate class com.dummy.package.ClassUnderTst
at cucumber.runtime.java.DefaultJavaObjectFactory.cacheNewInstance(DefaultJavaObjectFactory.java:46)
at cucumber.runtime.java.DefaultJavaObjectFactory.getInstance(DefaultJavaObjectFactory.java:32)
at cucumber.runtime.java.JavaStepDefinition.execute(JavaStepDefinition.java:38)
....
....
Caused by: java.lang.reflect.InvocationTargetException
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
at cucumber.runtime.java.DefaultJavaObjectFactory.cacheNewInstance(DefaultJavaObjectFactory.java:40)
... 16 more
Caused by: org.mockito.exceptions.misusing.MissingMethodInvocationException:
when() requires an argument which has to be 'a method call on a mock'.
For example:
when(mock.getArticles()).thenReturn(articles);
Also, this error might show up because:
1. you stub either of: final/private/equals()/hashCode() methods.
Those methods *cannot* be stubbed/verified.
Mocking methods declared on non-public parent classes is not supported.
2. inside when() you don't call method on mock but on some other object.
at com.dummy.package.ClassUnderTst.<init>(ClassUnderTst.java:60)
... 21 more