【发布时间】:2021-01-14 09:33:01
【问题描述】:
我的配置摘录
<...>
@Bean(name = Bus.DEFAULT_BUS_ID)
public SpringBus springBus() {
SpringBus springBus = new SpringBus();
LoggingInInterceptor ipt = new LoggingInInterceptor();
LoggingOutInterceptor opt = new LoggingOutInterceptor();
ipt.setPrettyLogging(true);
opt.setPrettyLogging(true);
springBus.getInInterceptors().add(ipt);
springBus.getOutInterceptors().add(opt);
return springBus;
}
@Bean
public MyService getMyServiceProxy() {
JaxWsProxyFactoryBean jaxWsProxyFactoryBean =
new JaxWsProxyFactoryBean();
jaxWsProxyFactoryBean.setAddress(MyServiceAddress + ":" +
MyServicePort + MyServiceAddressPath);
jaxWsProxyFactoryBean.setServiceClass(MyService.class);
jaxWsProxyFactoryBean.setBus(springBus());
return jaxWsProxyFactoryBean.create(MyService.class);
}
@Bean
public Client myServiceClentProxy() {
return ClientProxy.getClient(getMyServiceProxy());
}
@Bean
public HTTPConduit myServiceAgentConduit() {
HTTPConduit httpConduit =
(HTTPConduit) myServiceClentProxy().getConduit();
httpConduit.setAuthorization(basicAuthorization());
return httpConduit;
}
@Bean
public AuthorizationPolicy basicAuthorization() {
AuthorizationPolicy authorizationPolicy =
new AuthorizationPolicy();
authorizationPolicy.setUserName(myServiceUser);
authorizationPolicy.setPassword(myServicePassword);
authorizationPolicy.setAuthorizationType("Basic");
return authorizationPolicy;
}
<...>
当我尝试正常运行我的应用程序时,一切正常,但是当我尝试运行测试时,它会抛出一个 在进行配置的阶段失败。
Caused by: java.lang.IllegalArgumentException: not a proxy instance
完全错误:
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'myServiceClientProxy' defined in class path resource [MyServiceConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.apache.cxf.endpoint.Client]: Factory method 'myServiceClientProxy' threw exception; nested exception is java.lang.IllegalArgumentException: not a proxy instance
如何解决这个问题?除了 mockito 和 Junit 之外,我还需要其他库吗?
第 1 次更新
测试用例甚至与 cxf 本身无关
@ExtendWith(SpringExtension.class)
@ExtendWith(MockitoExtension.class)
@ContextConfiguration(classes = {MyServiceController.class})
@SpringBootTest
@Import(MyServiceApplication.class)
public class ControllerTest {
@Autowired
private MyServiceController myServiceController;
@MockBean
private MyService myService;
@Test
public void doTest() {
when(myService.doAction(any(String.class))).thenReturn("empty");
}
}
关于类级别注释的第 2 条更新
@ExtendWith(SpringExtension.class)
@ExtendWith(MockitoExtension.class)
@ContextConfiguration(classes = {MyServiceController.class})
@SpringBootTest
@Import(MyServiceApplication.class)
如果我删除这些注释中的任何一个,我会收到一条消息
org.springframework.beans.factory.BeanCreationException: Error creating bean with name myService
更新 3 号 如果我删除 @MockBean 注释并符合条件
@Bean(name = "serviceProxy")
public RepositoryWS getMyServiceProxy() {}
和
@Bean(name = "clientProxy")
public Client myServiceClientProxy() {
return ClientProxy.getClient(getMyServiceProxy());
}
和
@ExtendWith(SpringExtension.class)
@ExtendWith(MockitoExtension.class)
@ContextConfiguration(classes = {MyServiceController.class})
@SpringBootTest
@Import(MyServiceApplication.class)
public class ControllerTest {
@Autowired
private MyServiceController myServiceController;
@MockBean
private MyService myService;
@Test
public void doTest() {
<deleteThisLine>when(myService.doAction(any(String.class))).thenReturn("empty");</deleteThisLine>
}
}
如果我使用@Autowired 而不是@MockBean,那么一切正常。 我对为什么模拟不起作用感到困惑。
第 4 次更新
这里似乎发生了错误:
包:java.lang.reflect;
类:Proxy.class
/*
* Verify that the object is actually a proxy instance.
*/
if (!isProxyClass(proxy.getClass())) {
throw new IllegalArgumentException("not a proxy instance");
}
【问题讨论】:
-
您何时/何处收到此错误?你只提到一个测试?请添加您的测试。
-
这甚至不会编译,带有注释负载的整个测试也没有任何意义。你想通过这个测试完成什么?
-
抱歉给您带来不便,我更新了帖子
-
如前所述,测试没有任何意义,有太多的注释相互干扰。将它们全部删除,只留下
@SpringBootTest或放弃使用@WebMvcTest(MyServiceCOntroller.class)。 -
当我按照你说的做时,它无法自动装配控制器类和 myService 类
标签: spring-boot mockito cxf junit5