Web Service Client

Now lets look at the consumer of the Web Service, on our case the junit testcase WebServiceTest.java

  • We already saw the consumers created dynamically using cxf in the Spring configuration, we are just going to use the injected clients directly in the testcase.
  • This testcase is run using SpringJUnit4ClassRunner which autowires beans using annotations
    @RunWith(SpringJUnit4ClassRunner.class)
    @ContextConfiguration(locations = { "classpath:/application-context.xml" })
    public class WebServiceTest {
    
  • We have autowired the HelloWorld.java and the FileManagement.java beans
    	@Autowired
    	@Qualifier("helloClient")
    	protected HelloWorld helloWorld;
    
    	@Autowired
    	@Qualifier("fileClient")
    	protected FileManagement fileManagement;
    
  • The callSayHi test method calls the HelloWorld.java with a string parameter
    	@Test
    	public void callSayHi() throws Exception {
    		System.out.println(helloWorld.sayHi("HelloWorldTest"));
    	}
    
  • The callUpload test method calls the FileManagement.java with the File.java object as the parameter
    	@Test
    	public void callUpload() throws Exception {
    		File file = new File();
    		java.io.File fileToUpload = new java.io.File(
    				"src/test/resources/icons.jpg");
    		file.setFileName(fileToUpload.getName());
    		FileDataSource fileDataSource = new FileDataSource(fileToUpload);
    		DataHandler fileContent = new DataHandler(fileDataSource);
    		file.setFileContent(fileContent);
    		System.out.println(fileManagement.upload(file));
    	}
    }
    
  • That's it, we have consumed the web service with as little code as possible. happy coding :-)

Who's online

There are currently 0 users and 1 guest online.

Who's new

  • Saravana Peruma...