首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >JUnit for Spring WebServiceTemplate不工作

JUnit for Spring WebServiceTemplate不工作
EN

Stack Overflow用户
提问于 2017-04-19 03:03:07
回答 1查看 3.7K关注 0票数 1

我正在使用Spring服务来使用Spring。在执行JUnit时,我面临一个问题。

我的客户端类:

代码语言:javascript
复制
@Service
public class MyWsClient extends WebServiceGatewaySupport {

@Autowired
private WebServiceTemplate webServiceTemplate;

public String getData(...) throws Exception{
    ...
    SampleResponse response = (SampleResponse) webServiceTemplate.marshalSendAndReceive(request);
    ...
    return response.getData();
}}

我的测试类:

代码语言:javascript
复制
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath:applicationContext-junit.xml"})
public class MyWsClientTest {

@Autowired
ApplicationContext applicationContext;

@InjectMocks
MyWsClient myWsClient;

private MockWebServiceServer mockServer;

@Before
public void init() {
    MockitoAnnotations.initMocks(this);
    mockServer = MockWebServiceServer.createServer(applicationContext);
}

@Test
public void testGetData() throws Exception {

    Source expectedRequestPayload = new StringSource(
            "<customerCountRequest xmlns=\"http://springframework.org/spring-ws/test\" />");
    Source responsePayload = new StringSource(
            "<customerCountResponse xmlns='http://springframework.org/spring-ws/test'>"
                    + "<customerCount>10</customerCount>" + "</customerCountResponse>");
    mockServer.expect(RequestMatchers.payload(expectedRequestPayload)).andRespond(ResponseCreators.withPayload(responsePayload));

    String data = myWsClient.getData(...);
    assertNotNull(data);
    mockServer.verify();
}}

发行:

当我执行我的测试用例时,我在我的客户机类中为行"webServiceTemplate.marshalSendAndReceive(request)“获取NullPointerException。

webServiceTemplate将以null形式出现。

知道我做错了什么吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-04-19 07:43:42

我通过将"MyWsClient myWsClient“上的@InjectMocks改为@Autowired来解决这个问题;

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/43485599

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档