我正在尝试使用AutowireCapableBeanFactory镜像已自动运行外部服务资源的Java测试。这是我想出的办法,直到需要自动关联有问题的服务的依赖项时,它才能完成工作。
我还尝试过为Java和Scala套件都提供一个Java父类,该类可以自动装配必需的bean,由于Java指针的某些空指针,对于Java版本来说,它工作得很好,但对于Scala版本而言,效果并不好。
class RequestSuite extends PropSpec
with TableDrivenPropertyChecks with Matchers with BeforeAndAfterEach with BeforeAndAfterAll {
var mockServer: ClientAndServer = _
var proxy: ClientAndProxy = _
var resource = new SpringBeanResourceThatWorksFineOnOtherJavaClass
import BaseTest._
setupData()
ctx.getAutowireCapableBeanFactory.autowireBean(resource)
val testResp = resource.create(request)
override def beforeAll: Unit = {
mockServer = startClientAndServer(1081)
proxy = ClientAndProxy.startClientAndProxy(1091)
}
override def afterAll: Unit = {
mockServer.stop
proxy.stop
}
val testExample = {...}
property("Attributes should match for each example value") {...}
class CallBackTest extends ExpectationCallback {
def mockResponse(httpRequest: HttpRequest): HttpResponse = response.withStatusCode(200)
.withHeaders(header("Content-Type", "application/xml"))
override def handle(httpRequest: HttpRequest): HttpResponse = mockResponse(httpRequest)
}
}
如果我运行此命令,我会收到一条很长的错误消息,例如
异常或错误导致运行中止:使用以下命令创建bean时出错 名称 'SpringBeanResourceThatWorksFineOnOtherJavaClass': 自动连接的依赖项注入失败;嵌套异常为 org.springframework.beans.factory.BeanCreationException:无法 自动连线栏位:私人 AutowiredDependencyWithinThisService
嵌套的例外是 org.springframework.beans.factory.NoSuchBeanDefinitionException:否 类型的合格豆 [此服务中的AutowiredDependencyWithin] 找到依赖关系:至少需要1个符合条件的bean 自动关联此依赖项的候选对象。依赖注释: {@ org.springframework.beans.factory.annotation.Autowired(required = true)}
为了进行比较,这是成功运行的Java版本摘要
public class TestSuite extends BaseTest {
private static ClientAndProxy proxy;
private static ClientAndServer mockServer;
SameServiceThatWorksFineWithTheJavaVersion resource = new SameServiceThatWorksFineWithTheJavaVersion();
@Autowired
private AutowireCapableBeanFactory beanFactory;
@BeforeClass
public static void startProxy() {
mockServer = startClientAndServer(1080);
proxy = ClientAndProxy.startClientAndProxy(1090);
}
@AfterClass
public static void stopProxy() {
proxy.stop();
mockServer.stop();
}
@Test
public void testStuff() throws SomeException {...}
private void testStuffHelper() {
...
beanFactory.autowireBean(resource);
TestResp testResp = resource.doStuff(request);
...
}