简体   繁体   中英

Spring boot integrationTest web configuration

Trying implement integration test for httpClient. HttpClient can use stub rest controller service to send data for "other web".

Configuration is:

@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = {
     SpringMvcApplicationConfiguration.class
    , StorageConfiguration.class
    , …
})
@WebIntegrationTest({"server.port=8080", "management.port=0"})
@Transactional
public class HttpSendHelperTest {
  private final static Logger LOGGER =     LoggerFactory.getLogger(HttpSendHelperTest.class);

  @Inject
  private HttpSendHelper httpSendHelper;
  @Inject
  private RequestMappingHandlerMapping mapping;
…
}

Test starts correctly loading all my configured contests. Checking mapping.getHandlerMethods() - all uris present in map.

Sending test request to default url - response status is 200, but sending to others (trying some from mapped uris) - response status is 404.

RestTemplate restTemplate = new TestRestTemplate();


restTemplate.postForEntity("http://localhost:8080/",
    StubBuilder.getInspection(), String.class)

responseEntity = restTemplate.postForEntity("http://localhost:8080/stub/send",
    StubBuilder.getInspection(), String.class);

Please suggest how to solve the problem.

Solution

It needs to configure springBoot in correct way.

@RunWith(SpringJUnit4ClassRunner.class)
@WebIntegrationTest({"server.port=8080", "management.port=0"})
@Transactional
@SpringApplicationConfiguration(classes = SpringBootApplicationForTest.class)
public class HttpSendHelperTest {

adding SpringBootApplicationForTest.class

@EnableConfigurationProperties(value =
                                   {
                                       StorageConfiguration.class
                                   }
)
@EnableAutoConfiguration(exclude = {
    DataSourceAutoConfiguration.class
    , VelocityAutoConfiguration.class
})
@EntityScan(basePackages = {
    "my.entities.path"
})
@SpringBootApplication
public class SpringBootApplicationForTest {
  public static void main(String[] args) {
    SpringApplication.run(SpringBootApplicationForTest.class, args);
  }

}

Great thanks for Petri Kainulainen (helpful advices).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM