Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save maciejwalkowiak/65f25ee55b72b745986f to your computer and use it in GitHub Desktop.
Save maciejwalkowiak/65f25ee55b72b745986f to your computer and use it in GitHub Desktop.
Spring WS Starter Sample Tests
package sample.ws;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.test.IntegrationTest;
import org.springframework.boot.test.SpringApplicationConfiguration;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.ws.client.core.WebServiceTemplate;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;
import java.io.StringReader;
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = { SampleWsApplication.class, SampleWsApplicationTests.MockConfig.class })
@WebAppConfiguration
@IntegrationTest
@DirtiesContext
public class SampleWsApplicationSmokeTests {
private WebServiceTemplate webServiceTemplate = new WebServiceTemplate();
@Value("${local.server.port}")
private int serverPort;
@Before
public void setUp() {
webServiceTemplate.setDefaultUri("http://localhost:" + serverPort + "/services/");
}
@Test
public void foo() {
// given
final String startDate = "2013-10-20";
final String endDate = "2013-11-22";
final String firstName = "John";
final String lastName = "Doe";
final Integer employeeNumber = 1;
final String request = createRequest(startDate, endDate, employeeNumber, firstName, lastName);
StreamSource source = new StreamSource(new StringReader(request));
StreamResult result = new StreamResult(System.out);
// when
webServiceTemplate.sendSourceAndReceiveToResult(source, result);
}
private String createRequest(String startDate, String endDate, Integer employeeNumber, String firstName, String lastName) {
return "<hr:HolidayRequest xmlns:hr=\"http://mycompany.com/hr/schemas\">"
+ " <hr:Holiday>"
+ " <hr:StartDate>" + startDate + "</hr:StartDate>"
+ " <hr:EndDate>" + endDate + "</hr:EndDate>"
+ " </hr:Holiday>"
+ " <hr:Employee>"
+ " <hr:Number>" + employeeNumber + "</hr:Number>"
+ " <hr:FirstName>" + firstName + "</hr:FirstName>"
+ " <hr:LastName>" + lastName + "</hr:LastName>"
+ " </hr:Employee>"
+ "</hr:HolidayRequest>";
}
}
package sample.ws;
import org.hamcrest.Description;
import org.hamcrest.Matcher;
import org.hamcrest.TypeSafeMatcher;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.test.IntegrationTest;
import org.springframework.boot.test.SpringApplicationConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.ws.client.core.WebServiceTemplate;
import sample.ws.service.HumanResourceService;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;
import java.io.StringReader;
import java.text.SimpleDateFormat;
import java.util.Date;
import static org.mockito.Matchers.argThat;
import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.*;
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = { SampleWsApplication.class, SampleWsApplicationTests.MockConfig.class })
@WebAppConfiguration
@IntegrationTest
@DirtiesContext
public class SampleWsApplicationTests {
private static final String DATE_FORMAT = "yyyy-MM-dd";
@Configuration
public static class MockConfig {
@Bean
@Primary
public HumanResourceService humanResourceService() {
return mock(HumanResourceService.class);
}
}
private WebServiceTemplate webServiceTemplate = new WebServiceTemplate();
@Autowired
private HumanResourceService humanResourceService;
@Value("${local.server.port}")
private int serverPort;
@Before
public void setUp() {
webServiceTemplate.setDefaultUri("http://localhost:" + serverPort + "/services/");
}
@Test
public void foo() {
// given
final String startDate = "2013-10-20";
final String endDate = "2013-11-22";
final String firstName = "John";
final String lastName = "Doe";
final Integer employeeNumber = 1;
final String request = createRequest(startDate, endDate, employeeNumber, firstName, lastName);
StreamSource source = new StreamSource(new StringReader(request));
StreamResult result = new StreamResult(System.out);
// when
webServiceTemplate.sendSourceAndReceiveToResult(source, result);
// then
verify(humanResourceService).bookHoliday(argThat(dateMatches(startDate)), argThat(dateMatches(endDate)), eq(firstName + " " + lastName));
}
private String createRequest(String startDate, String endDate, Integer employeeNumber, String firstName, String lastName) {
return "<hr:HolidayRequest xmlns:hr=\"http://mycompany.com/hr/schemas\">"
+ " <hr:Holiday>"
+ " <hr:StartDate>" + startDate + "</hr:StartDate>"
+ " <hr:EndDate>" + endDate + "</hr:EndDate>"
+ " </hr:Holiday>"
+ " <hr:Employee>"
+ " <hr:Number>" + employeeNumber + "</hr:Number>"
+ " <hr:FirstName>" + firstName + "</hr:FirstName>"
+ " <hr:LastName>" + lastName + "</hr:LastName>"
+ " </hr:Employee>"
+ "</hr:HolidayRequest>";
}
private Matcher<Date> dateMatches(final String date) {
final SimpleDateFormat simpleDateFormat = new SimpleDateFormat(DATE_FORMAT);
return new TypeSafeMatcher<Date>() {
@Override protected boolean matchesSafely(Date item) {
return date.equals(simpleDateFormat.format(item));
}
@Override public void describeTo(Description description) {
description.appendText("Date equal to " + date);
}
};
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment