使用Kotlin的TestRestTemplate设置端口-1

时间:2018-07-23 15:28:45

标签: spring spring-boot kotlin integration-testing

我在配置文件中有一个声明的bean

  @Bean
  fun testRestTemplate(): TestRestTemplate {
    val templateBuilder = RestTemplateBuilder()
        .rootUri("http://localhost:8080")

    return TestRestTemplate(templateBuilder, null, null,
        TestRestTemplate.HttpClientOption.ENABLE_COOKIES)
  }

这是我的整合测试

@RunWith(SpringJUnit4ClassRunner::class)
@ContextConfiguration(classes = [AmberTestConfiguration::class])
@Tag("integration")
class IntegrationTests {
  val initData: List<CategoryDTO> = getCategoryDummyData()

  @Autowired lateinit var testRestTemplate: TestRestTemplate

  @Test
  fun create_categoryWithoutParent_ok_test() {
    val newCategory = NewCategoryRequest(
        name = "Test_Success",
        description = Optional.of("Test category, integration test"),
        parents = listOf(),
        domain = "treasury"
    )

    val response: ResponseEntity<Category> = testRestTemplate
        .postForEntity("/api/category", newCategory,
        Category::class.java)

    assertNotNull(response)
    assertEquals(HttpStatus.OK, response.statusCode)

    val body = response.body
    assertNotNull(body)
    assertNotNull(body.id)
    assertEquals(newCategory.name, body.name)
    assertEquals(newCategory.description.get(), body.description)
    assertEquals(toGUIDList(newCategory.parents), body.parents)
  }

我收到此错误:

org.springframework.web.client.ResourceAccessException: I/O error on POST request for "http://localhost/-1/api/category": Connection refused: connect; nested exception is java.net.ConnectException: Connection refused: connect

当我检查rooUri时:localhost:-1

我尝试使用RestTemplate。我也尝试在集成测试中使用@SpringBootTest。 我尝试在postForEntity中使用完整的网址。

0 个答案:

没有答案