Example with @EnableWebMvc

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Example with @EnableWebMvc

hlopez
Do you have an example how to test an SpringBoot class that uses @EnableWebMvc?

I'm trying out the following:
@EnableWebMvc
@WebAppConfiguration
@ContextConfiguration(classes = {AppConfig.class, MyController.class})
@TestPropertySource(properties = {"my.ip.address=localhost", "other.property=OTHER_PROP"}) //To bypass spring-cloud-config
public class MyAppIT extends TestNGCitrusTestRunner {
....

I had to add @WebAppConfiguration other wise I was getting an error, but now my test is still failing, cause I thought the Citrus httpClient should send to localhost:8080 but I keep getting this error:

Connect to localhost:8080 [localhost/127.0.0.1, localhost/0:0:0:0:0:0:0:1] failed: Connection refused: connect; nested exception is org.apache.http.conn.HttpHostConnectException: Connect to localhost:8080 [localhost/127.0.0.1, localhost/0:0:0:0:0:0:0:1] failed: Connection refused: connect

I've tried to see on which port it is starting the the server but I can see it in the logging, I guess my @EnableWebMvc or @WebAppConfiguration is not starting a tomcat server on port 8080?
But I do see the following from my controller:

02-01-2018 13:58:41.922 [main] INFO  o.s.w.s.m.m.a.RequestMappingHandlerMapping.register - Mapped "{[/myapp],methods=[POST]}" onto public void com.company.demo.api.controller.MyController.putById(java.lang.Integer,java.lang.String)
02-01-2018 13:58:41.922 [main] INFO  o.s.w.s.m.m.a.RequestMappingHandlerMapping.register - Mapped "{[/myapp],methods=[DELETE]}" onto public void com.company.demo.api.controller.MyController.deleteById(java.lang.Integer)
02-01-2018 13:58:41.922 [main] INFO  o.s.w.s.m.m.a.RequestMappingHandlerMapping.register - Mapped "{[/myapp],methods=[GET]}" onto public java.lang.String com.company.demo.api.controller.MyController.getById(java.lang.Integer)
Reply | Threaded
Open this post in threaded view
|

Re: Example with @EnableWebMvc

hlopez
I manage to get the http server started the following way:

@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT, properties = {"my.ip.address=localhost", "other.property=OTHER_PROP"})

But now I have the following problem, the properties are not set up front, like with

@TestPropertySource(properties = {"my.ip.address=localhost", "other.property=OTHER_PROP"})

Was helping me to bypass spring-cloud-config, but now it's not bypassing it anymore.
Seems like @SpringBootTest fixes one issue but raises another. Using it in combination @TestPropertySource did not give any effect, I do get the errors that it cannot connect to spring-cloud (cause it's not started), but it shouldn't cause I'm passing them as properties.
Reply | Threaded
Open this post in threaded view
|

Re: Example with @EnableWebMvc

hlopez
Managed to test it the following way:

@ActiveProfiles(profiles = {"test"})
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT, classes = {MyApplication.class})
public class MyAppIT extends TestNGCitrusTestRunner {

Setting the @ActiveProfiles to 'test' will make it use bootstrap-test.properties from your test/resources location which will override the normal bootstrap.properties in main/resources.
I also added spring.cloud.config.enabled=false in the bootstrap-test.properties to disable spring-cloud-config and added the properties needed ("my.ip.address=localhost", "other.property=OTHER_PROP") to the bootstrap-test.properties