Spring Boot + junit 5 + mockito + cxf Caused by: java.lang.IllegalArgumentException: not a proxy instance
SpringBoot + junit5 + mockito + cxf Caused by: java.lang.IllegalArgumentException: not a proxy instance
我的配置摘录
<...>
@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 之外我还需要任何其他库吗?
更新一号
测试用例甚至与 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");
}
}
关于class级注释的第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号更新
好像是这里出错了:
package: java.lang.reflect;
class: Proxy.class
/*
* Verify that the object is actually a proxy instance.
*/
if (!isProxyClass(proxy.getClass())) {
throw new IllegalArgumentException("not a proxy instance");
}
米。 Deinum 在我的 ControllerTest class 上是正确的,删除了所有
注释,但是 @SpringBootTest,我还必须专门为
测试用例
@SpringBootTest(classes = {MyServiceApplicationTest.class})
public class ControllerTest {
@Autowired
private MyServiceController myServiceController;
@MockBean
private MyService myService;
@Test
public void doTest() {
when(myService.doAction(any(String.class))).thenReturn("empty");
}
}
我的配置摘录
<...>
@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 之外我还需要任何其他库吗?
更新一号
测试用例甚至与 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");
}
}
关于class级注释的第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号更新
好像是这里出错了:
package: java.lang.reflect;
class: Proxy.class
/*
* Verify that the object is actually a proxy instance.
*/
if (!isProxyClass(proxy.getClass())) {
throw new IllegalArgumentException("not a proxy instance");
}
米。 Deinum 在我的 ControllerTest class 上是正确的,删除了所有 注释,但是 @SpringBootTest,我还必须专门为 测试用例
@SpringBootTest(classes = {MyServiceApplicationTest.class})
public class ControllerTest {
@Autowired
private MyServiceController myServiceController;
@MockBean
private MyService myService;
@Test
public void doTest() {
when(myService.doAction(any(String.class))).thenReturn("empty");
}
}