简体   繁体   中英

Spring Autowired in Web Service not working when tested

I'm trying to configure a web service client like this:

@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:

@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:

@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:

 @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.

Don't declare the variable WebServiceTemplate. Use getWebServiceTemplate(), inherited from WebServiceGatewaySupport, instead.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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