org.springframework.test.web.Person Java Examples

The following examples show how to use org.springframework.test.web.Person. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: SampleTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void performGetManyTimes() {

	String responseBody = "{\"name\" : \"Ludwig van Beethoven\", \"someDouble\" : \"1.6035\"}";

	this.mockServer.expect(manyTimes(), requestTo("/composers/42")).andExpect(method(HttpMethod.GET))
			.andRespond(withSuccess(responseBody, MediaType.APPLICATION_JSON));

	@SuppressWarnings("unused")
	Person ludwig = this.restTemplate.getForObject("/composers/{id}", Person.class, 42);

	// We are only validating the request. The response is mocked out.
	// hotel.getId() == 42
	// hotel.getName().equals("Holiday Inn")

	this.restTemplate.getForObject("/composers/{id}", Person.class, 42);
	this.restTemplate.getForObject("/composers/{id}", Person.class, 42);
	this.restTemplate.getForObject("/composers/{id}", Person.class, 42);

	this.mockServer.verify();
}
 
Example #2
Source File: XmlContentRequestMatchersIntegrationTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Before
public void setup() {
	List<Person> composers = Arrays.asList(
			new Person("Johann Sebastian Bach").setSomeDouble(21),
			new Person("Johannes Brahms").setSomeDouble(.0025),
			new Person("Edvard Grieg").setSomeDouble(1.6035),
			new Person("Robert Schumann").setSomeDouble(Double.NaN));

	this.people = new PeopleWrapper(composers);

	List<HttpMessageConverter<?>> converters = new ArrayList<>();
	converters.add(new Jaxb2RootElementHttpMessageConverter());

	this.restTemplate = new RestTemplate();
	this.restTemplate.setMessageConverters(converters);

	this.mockServer = MockRestServiceServer.createServer(this.restTemplate);
}
 
Example #3
Source File: FilterTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test // SPR-16067, SPR-16695
public void filterWrapsRequestResponseAndPerformsAsyncDispatch() throws Exception {
	MockMvc mockMvc = standaloneSetup(new PersonController())
			.addFilters(new WrappingRequestResponseFilter(), new ShallowEtagHeaderFilter())
			.build();

	MvcResult mvcResult = mockMvc.perform(get("/persons/1").accept(MediaType.APPLICATION_JSON))
			.andExpect(request().asyncStarted())
			.andExpect(request().asyncResult(new Person("Lukas")))
			.andReturn();

	mockMvc.perform(asyncDispatch(mvcResult))
			.andExpect(status().isOk())
			.andExpect(header().longValue("Content-Length", 53))
			.andExpect(header().string("ETag", "\"0e37becb4f0c90709cb2e1efcc61eaa00\""))
			.andExpect(content().string("{\"name\":\"Lukas\",\"someDouble\":0.0,\"someBoolean\":false}"));
}
 
Example #4
Source File: SampleAsyncTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void performGet() throws Exception {

	String responseBody = "{\"name\" : \"Ludwig van Beethoven\", \"someDouble\" : \"1.6035\"}";

	this.mockServer.expect(requestTo("/composers/42")).andExpect(method(HttpMethod.GET))
		.andRespond(withSuccess(responseBody, MediaType.APPLICATION_JSON));

	@SuppressWarnings("unused")
	ListenableFuture<ResponseEntity<Person>> ludwig =
			this.restTemplate.getForEntity("/composers/{id}", Person.class, 42);

	// We are only validating the request. The response is mocked out.
	// person.getName().equals("Ludwig van Beethoven")
	// person.getDouble().equals(1.6035)

	this.mockServer.verify();
}
 
Example #5
Source File: XpathRequestMatchersIntegrationTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Before
public void setup() {
	List<Person> composers = Arrays.asList(
			new Person("Johann Sebastian Bach").setSomeDouble(21),
			new Person("Johannes Brahms").setSomeDouble(.0025),
			new Person("Edvard Grieg").setSomeDouble(1.6035),
			new Person("Robert Schumann").setSomeDouble(Double.NaN));

	List<Person> performers = Arrays.asList(
			new Person("Vladimir Ashkenazy").setSomeBoolean(false),
			new Person("Yehudi Menuhin").setSomeBoolean(true));

	this.people = new PeopleWrapper(composers, performers);

	List<HttpMessageConverter<?>> converters = new ArrayList<>();
	converters.add(new Jaxb2RootElementHttpMessageConverter());

	this.restTemplate = new RestTemplate();
	this.restTemplate.setMessageConverters(converters);

	this.mockServer = MockRestServiceServer.createServer(this.restTemplate);
}
 
Example #6
Source File: SampleAsyncTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void performGetWithResponseBodyFromFile() throws Exception {

	Resource responseBody = new ClassPathResource("ludwig.json", this.getClass());

	this.mockServer.expect(requestTo("/composers/42")).andExpect(method(HttpMethod.GET))
		.andRespond(withSuccess(responseBody, MediaType.APPLICATION_JSON));

	@SuppressWarnings("unused")
	ListenableFuture<ResponseEntity<Person>> ludwig =
			this.restTemplate.getForEntity("/composers/{id}", Person.class, 42);

	// hotel.getId() == 42
	// hotel.getName().equals("Holiday Inn")

	this.mockServer.verify();
}
 
Example #7
Source File: AsyncTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@RequestMapping(params = "deferredResultWithDelayedError")
public DeferredResult<Person> getDeferredResultWithDelayedError() {
	final DeferredResult<Person> deferredResult = new DeferredResult<>();
	new Thread() {
		public void run() {
			try {
				Thread.sleep(100);
				deferredResult.setErrorResult(new RuntimeException("Delayed Error"));
			}
			catch (InterruptedException e) {
				/* no-op */
			}
		}
	}.start();
	return deferredResult;
}
 
Example #8
Source File: SampleTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test // SPR-14694
public void repeatedAccessToResponseViaResource() {

	Resource resource = new ClassPathResource("ludwig.json", this.getClass());

	RestTemplate restTemplate = new RestTemplate();
	restTemplate.setInterceptors(Collections.singletonList(new ContentInterceptor(resource)));

	MockRestServiceServer mockServer = MockRestServiceServer.bindTo(restTemplate)
			.ignoreExpectOrder(true)
			.bufferContent()  // enable repeated reads of response body
			.build();

	mockServer.expect(requestTo("/composers/42")).andExpect(method(HttpMethod.GET))
			.andRespond(withSuccess(resource, MediaType.APPLICATION_JSON));

	restTemplate.getForObject("/composers/{id}", Person.class, 42);

	mockServer.verify();
}
 
Example #9
Source File: SampleAsyncTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void performGetManyTimes() throws Exception {

	String responseBody = "{\"name\" : \"Ludwig van Beethoven\", \"someDouble\" : \"1.6035\"}";

	this.mockServer.expect(manyTimes(), requestTo("/composers/42")).andExpect(method(HttpMethod.GET))
			.andRespond(withSuccess(responseBody, MediaType.APPLICATION_JSON));

	@SuppressWarnings("unused")
	ListenableFuture<ResponseEntity<Person>> ludwig =
			this.restTemplate.getForEntity("/composers/{id}", Person.class, 42);

	// We are only validating the request. The response is mocked out.
	// person.getName().equals("Ludwig van Beethoven")
	// person.getDouble().equals(1.6035)

	this.restTemplate.getForEntity("/composers/{id}", Person.class, 42);
	this.restTemplate.getForEntity("/composers/{id}", Person.class, 42);
	this.restTemplate.getForEntity("/composers/{id}", Person.class, 42);
	this.restTemplate.getForEntity("/composers/{id}", Person.class, 42);

	this.mockServer.verify();
}
 
Example #10
Source File: SampleAsyncTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void performGetWithResponseBodyFromFile() throws Exception {

	Resource responseBody = new ClassPathResource("ludwig.json", this.getClass());

	this.mockServer.expect(requestTo("/composers/42")).andExpect(method(HttpMethod.GET))
		.andRespond(withSuccess(responseBody, MediaType.APPLICATION_JSON));

	@SuppressWarnings("unused")
	ListenableFuture<ResponseEntity<Person>> ludwig =
			this.restTemplate.getForEntity("/composers/{id}", Person.class, 42);

	// hotel.getId() == 42
	// hotel.getName().equals("Holiday Inn")

	this.mockServer.verify();
}
 
Example #11
Source File: SampleTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void performGet() {

	String responseBody = "{\"name\" : \"Ludwig van Beethoven\", \"someDouble\" : \"1.6035\"}";

	this.mockServer.expect(requestTo("/composers/42")).andExpect(method(HttpMethod.GET))
		.andRespond(withSuccess(responseBody, MediaType.APPLICATION_JSON));

	@SuppressWarnings("unused")
	Person ludwig = this.restTemplate.getForObject("/composers/{id}", Person.class, 42);

	// We are only validating the request. The response is mocked out.
	// hotel.getId() == 42
	// hotel.getName().equals("Holiday Inn")

	this.mockServer.verify();
}
 
Example #12
Source File: XpathRequestMatchersIntegrationTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Before
public void setup() {
	List<Person> composers = Arrays.asList(
			new Person("Johann Sebastian Bach").setSomeDouble(21),
			new Person("Johannes Brahms").setSomeDouble(.0025),
			new Person("Edvard Grieg").setSomeDouble(1.6035),
			new Person("Robert Schumann").setSomeDouble(Double.NaN));

	List<Person> performers = Arrays.asList(
			new Person("Vladimir Ashkenazy").setSomeBoolean(false),
			new Person("Yehudi Menuhin").setSomeBoolean(true));

	this.people = new PeopleWrapper(composers, performers);

	List<HttpMessageConverter<?>> converters = new ArrayList<>();
	converters.add(new Jaxb2RootElementHttpMessageConverter());

	this.restTemplate = new RestTemplate();
	this.restTemplate.setMessageConverters(converters);

	this.mockServer = MockRestServiceServer.createServer(this.restTemplate);
}
 
Example #13
Source File: SampleTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void performGet() {

	String responseBody = "{\"name\" : \"Ludwig van Beethoven\", \"someDouble\" : \"1.6035\"}";

	this.mockServer.expect(requestTo("/composers/42")).andExpect(method(HttpMethod.GET))
		.andRespond(withSuccess(responseBody, MediaType.APPLICATION_JSON));

	@SuppressWarnings("unused")
	Person ludwig = this.restTemplate.getForObject("/composers/{id}", Person.class, 42);

	// We are only validating the request. The response is mocked out.
	// hotel.getId() == 42
	// hotel.getName().equals("Holiday Inn")

	this.mockServer.verify();
}
 
Example #14
Source File: SampleTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void performGetManyTimes() {

	String responseBody = "{\"name\" : \"Ludwig van Beethoven\", \"someDouble\" : \"1.6035\"}";

	this.mockServer.expect(manyTimes(), requestTo("/composers/42")).andExpect(method(HttpMethod.GET))
			.andRespond(withSuccess(responseBody, MediaType.APPLICATION_JSON));

	@SuppressWarnings("unused")
	Person ludwig = this.restTemplate.getForObject("/composers/{id}", Person.class, 42);

	// We are only validating the request. The response is mocked out.
	// hotel.getId() == 42
	// hotel.getName().equals("Holiday Inn")

	this.restTemplate.getForObject("/composers/{id}", Person.class, 42);
	this.restTemplate.getForObject("/composers/{id}", Person.class, 42);
	this.restTemplate.getForObject("/composers/{id}", Person.class, 42);

	this.mockServer.verify();
}
 
Example #15
Source File: AsyncTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
void onMessage(String name) {
	for (DeferredResult<Person> deferredResult : this.deferredResults) {
		deferredResult.setResult(new Person(name));
		this.deferredResults.remove(deferredResult);
	}
	for (ListenableFutureTask<Person> futureTask : this.futureTasks) {
		futureTask.run();
		this.futureTasks.remove(futureTask);
	}
}
 
Example #16
Source File: AsyncTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void deferredResultWithImmediateValue() throws Exception {
	MvcResult mvcResult = this.mockMvc.perform(get("/1").param("deferredResultWithImmediateValue", "true"))
			.andExpect(request().asyncStarted())
			.andExpect(request().asyncResult(new Person("Joe")))
			.andReturn();

	this.mockMvc.perform(asyncDispatch(mvcResult))
			.andExpect(status().isOk())
			.andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8))
			.andExpect(content().string("{\"name\":\"Joe\",\"someDouble\":0.0,\"someBoolean\":false}"));
}
 
Example #17
Source File: AsyncTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void callable() throws Exception {
	MvcResult mvcResult = this.mockMvc.perform(get("/1").param("callable", "true"))
			.andExpect(request().asyncStarted())
			.andExpect(request().asyncResult(new Person("Joe")))
			.andReturn();

	this.mockMvc.perform(asyncDispatch(mvcResult))
			.andExpect(status().isOk())
			.andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8))
			.andExpect(content().string("{\"name\":\"Joe\",\"someDouble\":0.0,\"someBoolean\":false}"));
}
 
Example #18
Source File: RedirectTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void getPerson() throws Exception {
	this.mockMvc.perform(get("/persons/Joe").flashAttr("message", "success!"))
		.andExpect(status().isOk())
		.andExpect(forwardedUrl("persons/index"))
		.andExpect(model().size(2))
		.andExpect(model().attribute("person", new Person("Joe")))
		.andExpect(model().attribute("message", "success!"))
		.andExpect(flash().attributeCount(0));
}
 
Example #19
Source File: SampleTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void expectNever() {

	String responseBody = "{\"name\" : \"Ludwig van Beethoven\", \"someDouble\" : \"1.6035\"}";

	this.mockServer.expect(once(), requestTo("/composers/42")).andExpect(method(HttpMethod.GET))
			.andRespond(withSuccess(responseBody, MediaType.APPLICATION_JSON));
	this.mockServer.expect(never(), requestTo("/composers/43")).andExpect(method(HttpMethod.GET))
			.andRespond(withSuccess(responseBody, MediaType.APPLICATION_JSON));

	this.restTemplate.getForObject("/composers/{id}", Person.class, 42);

	this.mockServer.verify();
}
 
Example #20
Source File: HeaderAssertionTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@RequestMapping("/persons/{id}")
public ResponseEntity<Person> showEntity(@PathVariable long id, WebRequest request) {
	return ResponseEntity
			.ok()
			.lastModified(this.timestamp)
			.header("X-Rate-Limiting", "42")
			.header("Vary", "foo", "bar")
			.body(new Person("Jason"));
}
 
Example #21
Source File: ModelAssertionTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Before
public void setup() {

	SampleController controller = new SampleController("a string value", 3, new Person("a name"));

	this.mockMvc = standaloneSetup(controller)
			.defaultRequest(get("/"))
			.alwaysExpect(status().isOk())
			.setControllerAdvice(new ModelAttributeAdvice())
			.build();
}
 
Example #22
Source File: RedirectTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@PostMapping
public String save(@Valid Person person, Errors errors, RedirectAttributes redirectAttrs) {
	if (errors.hasErrors()) {
		return "persons/add";
	}
	redirectAttrs.addAttribute("name", "Joe");
	redirectAttrs.addFlashAttribute("message", "success!");
	return "redirect:/persons/{name}";
}
 
Example #23
Source File: RedirectTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@PostMapping("/people")
public Object saveSpecial(@Valid Person person, Errors errors, RedirectAttributes redirectAttrs) {
	if (errors.hasErrors()) {
		return "persons/add";
	}
	redirectAttrs.addAttribute("name", "Joe");
	redirectAttrs.addFlashAttribute("message", "success!");
	return new StringBuilder("redirect:").append("/persons").append("/{name}");
}
 
Example #24
Source File: AsyncTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void callable() throws Exception {
	MvcResult mvcResult = this.mockMvc.perform(get("/1").param("callable", "true"))
			.andExpect(request().asyncStarted())
			.andExpect(request().asyncResult(new Person("Joe")))
			.andReturn();

	this.mockMvc.perform(asyncDispatch(mvcResult))
			.andExpect(status().isOk())
			.andExpect(content().contentType(MediaType.APPLICATION_JSON))
			.andExpect(content().string("{\"name\":\"Joe\",\"someDouble\":0.0,\"someBoolean\":false}"));
}
 
Example #25
Source File: AsyncTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void deferredResultWithImmediateValue() throws Exception {
	MvcResult mvcResult = this.mockMvc.perform(get("/1").param("deferredResultWithImmediateValue", "true"))
			.andExpect(request().asyncStarted())
			.andExpect(request().asyncResult(new Person("Joe")))
			.andReturn();

	this.mockMvc.perform(asyncDispatch(mvcResult))
			.andExpect(status().isOk())
			.andExpect(content().contentType(MediaType.APPLICATION_JSON))
			.andExpect(content().string("{\"name\":\"Joe\",\"someDouble\":0.0,\"someBoolean\":false}"));
}
 
Example #26
Source File: HeaderAssertionTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@RequestMapping("/persons/{id}")
public ResponseEntity<Person> showEntity(@PathVariable long id, WebRequest request) {
	return ResponseEntity
			.ok()
			.lastModified(this.timestamp)
			.header("X-Rate-Limiting", "42")
			.header("Vary", "foo", "bar")
			.body(new Person("Jason"));
}
 
Example #27
Source File: XmlContentAssertionTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@RequestMapping(value="/music/people")
public @ResponseBody PeopleWrapper getPeople() {

	List<Person> composers = Arrays.asList(
			new Person("Johann Sebastian Bach").setSomeDouble(21),
			new Person("Johannes Brahms").setSomeDouble(.0025),
			new Person("Edvard Grieg").setSomeDouble(1.6035),
			new Person("Robert Schumann").setSomeDouble(Double.NaN));

	return new PeopleWrapper(composers);
}
 
Example #28
Source File: SampleTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void expectNever() {

	String responseBody = "{\"name\" : \"Ludwig van Beethoven\", \"someDouble\" : \"1.6035\"}";

	this.mockServer.expect(once(), requestTo("/composers/42")).andExpect(method(HttpMethod.GET))
			.andRespond(withSuccess(responseBody, MediaType.APPLICATION_JSON));
	this.mockServer.expect(never(), requestTo("/composers/43")).andExpect(method(HttpMethod.GET))
			.andRespond(withSuccess(responseBody, MediaType.APPLICATION_JSON));

	this.restTemplate.getForObject("/composers/{id}", Person.class, 42);

	this.mockServer.verify();
}
 
Example #29
Source File: AsyncTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
void onMessage(String name) {
	for (DeferredResult<Person> deferredResult : this.deferredResults) {
		deferredResult.setResult(new Person(name));
		this.deferredResults.remove(deferredResult);
	}
	for (ListenableFutureTask<Person> futureTask : this.futureTasks) {
		futureTask.run();
		this.futureTasks.remove(futureTask);
	}
}
 
Example #30
Source File: ViewResolutionTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void testXmlOnly() throws Exception {
	Jaxb2Marshaller marshaller = new Jaxb2Marshaller();
	marshaller.setClassesToBeBound(Person.class);

	standaloneSetup(new PersonController()).setSingleView(new MarshallingView(marshaller)).build()
		.perform(get("/person/Corea"))
			.andExpect(status().isOk())
			.andExpect(content().contentType(MediaType.APPLICATION_XML))
			.andExpect(xpath("/person/name/text()").string(equalTo("Corea")));
}