|
|
|
@ -1430,38 +1430,35 @@ For example: |
|
|
|
} |
|
|
|
} |
|
|
|
---- |
|
|
|
---- |
|
|
|
|
|
|
|
|
|
|
|
TIP: The context loader guesses whether you want to test a web application or not (e.g. with |
|
|
|
TIP: The context loader guesses whether you want to test a web application or not (e.g. |
|
|
|
`MockMVC`) by looking for the `@WebAppConfiguration` annotation. (`MockMVC` and |
|
|
|
with `MockMVC`) by looking for the `@WebAppConfiguration` annotation. (`MockMVC` and |
|
|
|
`@WebAppConfiguration` are part of `spring-test`). |
|
|
|
`@WebAppConfiguration` are part of `spring-test`). |
|
|
|
|
|
|
|
|
|
|
|
If you want a web application to start up and listen on its normal |
|
|
|
If you want a web application to start up and listen on its normal port, so you can test |
|
|
|
port, so you can test it with HTTP (e.g. using `RestTemplate`) |
|
|
|
it with HTTP (e.g. using `RestTemplate`), annotate your test class (or one of its |
|
|
|
annotate your test class (or one of its superclasses) |
|
|
|
superclasses) with `@IntegrationTest`. This can be very useful because it means you can |
|
|
|
`@IntegrationTest`. This can be very useful because it means you can |
|
|
|
test the full stack of your application, but also inject its components into the test |
|
|
|
test the full stack of your application, but also inject its |
|
|
|
class and use them to assert the internal state of the application after an HTTP |
|
|
|
components into the test class and use them to assert the internal |
|
|
|
interaction. For Example: |
|
|
|
state of the application after an HTTP interaction. Example: |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
[source,java,indent=0,subs="verbatim,quotes,attributes"] |
|
|
|
[source,java,indent=0,subs="verbatim,quotes,attributes"] |
|
|
|
---- |
|
|
|
---- |
|
|
|
@RunWith(SpringJUnit4ClassRunner.class) |
|
|
|
@RunWith(SpringJUnit4ClassRunner.class) |
|
|
|
@SpringApplicationConfiguration(classes = SampleDataJpaApplication.class) |
|
|
|
@SpringApplicationConfiguration(classes = SampleDataJpaApplication.class) |
|
|
|
@WebApplication |
|
|
|
@WebApplication |
|
|
|
@IntegrationTest |
|
|
|
@IntegrationTest |
|
|
|
public class CityRepositoryIntegrationTests { |
|
|
|
public class CityRepositoryIntegrationTests { |
|
|
|
|
|
|
|
|
|
|
|
@Autowired |
|
|
|
@Autowired |
|
|
|
CityRepository repository; |
|
|
|
CityRepository repository; |
|
|
|
|
|
|
|
|
|
|
|
RestTemplate restTemplate = RestTemplates.get(); |
|
|
|
RestTemplate restTemplate = RestTemplates.get(); |
|
|
|
|
|
|
|
|
|
|
|
// ... interact with the running server |
|
|
|
// ... interact with the running server |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
---- |
|
|
|
---- |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
[[boot-features-test-utilities]] |
|
|
|
[[boot-features-test-utilities]] |
|
|
|
=== Test utilities |
|
|
|
=== Test utilities |
|
|
|
A few test utility classes are packaged as part of `spring-boot` that are generally |
|
|
|
A few test utility classes are packaged as part of `spring-boot` that are generally |
|
|
|
@ -1528,26 +1525,24 @@ public class MyTest { |
|
|
|
[[boot-features-rest-templates-test-utility]] |
|
|
|
[[boot-features-rest-templates-test-utility]] |
|
|
|
==== RestTemplates |
|
|
|
==== RestTemplates |
|
|
|
|
|
|
|
|
|
|
|
`RestTemplates` is a static convenience factory for instances of |
|
|
|
`RestTemplates` is a static convenience factory for instances of `RestTemplate` that are |
|
|
|
`RestTemplate` that are useful in integration tests. You can get a |
|
|
|
useful in integration tests. You can get a vanilla template or one that sends Basic HTTP |
|
|
|
vanilla template or one that sends Basic HTTP authentication (with a |
|
|
|
authentication (with a username and password). And in either case the template will behave |
|
|
|
username and password). And in either case the template will behave in |
|
|
|
in a friendly way for testing, not following redirects (so you can assert the response |
|
|
|
a friendly way for testing, not following redirects (so you can assert |
|
|
|
location), ignoring cookies (so the template is stateless), and not throwing exceptions |
|
|
|
the response location), ignoring cookies (so the template is |
|
|
|
on server-side errors. It is recommended, but not mandatory, to use Apache HTTP Client |
|
|
|
stateless), and not throwing exceptions on server-side errors. It is |
|
|
|
(version 4.3.2 or better), and if you have that on your classpath the `RestTemplates` will |
|
|
|
recommended, but not mandatory, to use Apache HTTP Client (version |
|
|
|
respond by configuring the client appropriately. |
|
|
|
4.3.2 or better), and if you have that on your classpath the |
|
|
|
|
|
|
|
`RestTemplates` will respond by configuring the client appropriately. |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
[source,java,indent=0] |
|
|
|
[source,java,indent=0] |
|
|
|
---- |
|
|
|
---- |
|
|
|
public class MyTest { |
|
|
|
public class MyTest { |
|
|
|
|
|
|
|
|
|
|
|
RestTemplate template = RestTemplates.get(); |
|
|
|
RestTemplate template = RestTemplates.get(); |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@Test |
|
|
|
public void testRequest() throws Exception { |
|
|
|
public void testRequest() throws Exception { |
|
|
|
HttpHeaders headers = template.getForEntity("http://myhost.com", String.class).getHeaders(); |
|
|
|
HttpHeaders headers = template.getForEntity("http://myhost.com", String.class).getHeaders(); |
|
|
|
assertThat(headers.getLocation().toString(), containsString("myotherhost")); |
|
|
|
assertThat(headers.getLocation().toString(), containsString("myotherhost")); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@ -1555,6 +1550,7 @@ public class MyTest { |
|
|
|
---- |
|
|
|
---- |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
[[boot-features-developing-auto-configuration]] |
|
|
|
[[boot-features-developing-auto-configuration]] |
|
|
|
== Developing auto-configuration and using conditions |
|
|
|
== Developing auto-configuration and using conditions |
|
|
|
If you work in a company that develops shared libraries, or if you work on an open-source |
|
|
|
If you work in a company that develops shared libraries, or if you work on an open-source |
|
|
|
|