Java 打破多态性?
如果没有显式强制转换,则无法执行来自实现接口的对象的方法 更广泛的背景是,我试图将重新发布的DSL断言修饰为在一个临时测试框架的测试报告中声明,因此代码如下:Java 打破多态性?,java,groovy,rest-assured,Java,Groovy,Rest Assured,如果没有显式强制转换,则无法执行来自实现接口的对象的方法 更广泛的背景是,我试图将重新发布的DSL断言修饰为在一个临时测试框架的测试报告中声明,因此代码如下: ~then().assertThat().statusCode(200).body(matcher) 在测试报告中做出这种意义上的陈述 至于decorator模式,我几乎没有成功(我假设我在开发方面的技能有限),因此我正在尝试另一种更简单的方法,即在then调用中传递TestReport对象,触及Rest-Assured代码库,因此现在
~then().assertThat().statusCode(200).body(matcher)
在测试报告中做出这种意义上的陈述
至于decorator模式,我几乎没有成功(我假设我在开发方面的技能有限),因此我正在尝试另一种更简单的方法,即在then调用中传递TestReport对象,触及Rest-Assured代码库,因此现在语法可以是:
~then(report).assertThat().statusCode(200).body(matcher)
直到我感到困惑的特定点,这显然是破坏了多态性,我尝试在特定类(RestAssuredResponseImplementation)设置断点,检查这是运行时的实际类型,并评估它在没有强制转换的情况下无法工作,从正确的方法抛出java.lang.AbstractMethodError?!!当然,这不是抽象的
- 涉及的片段包括:
- 对于测试定义:
提供了报告对象,并且,如果您强制转换可验证对象,将行更改为:@Test public void test1(){ Validatable validatable = given(). header("Authorization", "Basic ASDFqwertyssW8="). when(). get("https://some-site.org/login"); validatable.then(report). assertThat().statusCode(200); }
一切正常,否则我会遇到前面提到的例外:((RestAssuredResponseImpl)validatable).then(report).
T然后(报告);
私有TestCaseReport TestCaseReport;
ReportingValidableResponseImpl(字符串内容类型、ResponseParserRegistrar rpr、RestAssuredConfig配置、响应响应、可提取响应、可提取响应、LogRepository LogRepository、TestCaseReport TestCaseReport){
super(contentType、rpr、config、response、extractableResponse、logRepository);
this.testCaseReport=testCaseReport;
}
@凌驾
ValidatableResponse状态代码(int-expectedStatusCode){
testCaseReport.logMessage(LogStatus.INFO,“断言状态代码为”+expectedStatusCode)
super.statusCode(expectedStatusCode)
}
java.lang.AbstractMethodError: io.restassured.internal.RestAssuredResponseImpl.then(Report;)Lio/restassured/response/ValidatableResponseOptions;
ValidatableResponse then(TestCaseReport testCaseReport) {
return new ReportingValidatableResponseImpl(contentType, rpr, config, this, this, logRepository, testCaseReport);
}