简体   繁体   English

测试时Web Service中的Spring Autowired无法正常工作

[英]Spring Autowired in Web Service not working when tested

I'm trying to configure a web service client like this: 我正在尝试像这样配置Web服务客户端:

@EnableSwagger
@Configuration
@EnableAutoConfiguration
@ComponentScan
@EnableWebMvc
public class Application {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

With a second config class for the WS: 使用第二个WS的配置类:

@Configuration
@ComponentScan(basePackages = {"org.myco.myproj.core.endpoints"})
public class WebServiceConfig {

    @Bean
    public Jaxb2Marshaller marshaller() throws Exception {
        Jaxb2Marshaller marshaller = new Jaxb2Marshaller();
        marshaller.setContextPath("org.myco.myproj.core.webservices");
        return marshaller;
    }

    @Bean
    public WebServiceTemplate webServiceTemplate(Jaxb2Marshaller marshaller) {
        WebServiceTemplate webServiceTemplate = new WebServiceTemplate();
        webServiceTemplate.setMarshaller(marshaller);
        webServiceTemplate.setUnmarshaller(marshaller);
        webServiceTemplate.setDefaultUri("http://localhost:11000/ws/");
        return webServiceTemplate;
    }

    @Bean
    public AccountEndpoint accountEndpoint(Jaxb2Marshaller marshaller, WebServiceTemplate webServiceTemplate) {
        AccountEndpoint client = new AccountEndpoint(webServiceTemplate);
        client.setDefaultUri("http://localhost:11000/ws");
        client.setMarshaller(marshaller);
        client.setUnmarshaller(marshaller);
        return client;
    }
}

I've generate domain classes from the WSDL using JABX, and created a service endpoint like this: 我已经使用JABX从WSDL生成了域类,并创建了一个服务端点,如下所示:

@Service
public class AccountEndpoint extends WebServiceGatewaySupport {

    private static final Logger logger = Logger.getLogger(String.valueOf(AccountEndpoint.class));

    private WebServiceTemplate webServiceTemplate;

    public AccountEndpoint(WebServiceTemplate webServiceTemplate) {
        this.webServiceTemplate = webServiceTemplate;
    }

    public GetAccountResponse getAccount(long accountAgency, long accountNumber) {
        GetAccountRequest request = new GetAccountRequest();
        request.setAccountAgency(accountAgency);
        request.setAccountNumber(accountNumber);

        GetAccountResponse response = (GetAccountResponse)
                webServiceTemplate.marshalSendAndReceive(request);

        return response;
    }
}

I created a simple test to check if this is running, which is returning NullPointerException at the autowired field: 我创建了一个简单的测试来检查它是否正在运行,该测试在自动装配字段返回NullPointerException

 @ContextConfiguration("org.myco.myproj.config.WebServiceConfig")
    public class AccountEndpointTest extends TestCase {

        @Autowired
        private AccountEndpoint accountEndpoint;

        public void setUp() throws Exception {
            super.setUp();
        }

        @Test
        public void testGetAccount() throws Exception {

            GetAccountResponse response = accountEndpoint.getAccount(12, 16);

            assertNotNull(response);
        }
    }

What I'm missing? 我想念的是什么? Thanks. 谢谢。

Seem you are missing 好像你失踪了

@RunWith(SpringJUnit4ClassRunner.class)

from your test class. 从您的测试班。 Otherwise, you are running with the default JUnit test runner and Spring is not involved. 否则,您将使用默认的JUnit测试运行程序运行,并且不涉及Spring。

Don't declare the variable WebServiceTemplate. 不要声明变量WebServiceTemplate。 Use getWebServiceTemplate(), inherited from WebServiceGatewaySupport, instead. 改用从WebServiceGatewaySupport继承的getWebServiceTemplate()。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM