相关疑难解决方法(0)

Spring Boot - 如何获取正在运行的端口

我有一个spring boot应用程序(使用嵌入式tomcat 7),我已经设置server.port = 0了我的application.properties所以我可以有一个随机端口.服务器启动并在端口上运行后,我需要能够获得所选的端口.

我不能使用,@Value("$server.port")因为它是零.这是一个看似简单的信息,为什么我不能从我的java代码中访问它?我该如何访问它?

port spring embedded-tomcat-7 spring-boot

62
推荐指数
8
解决办法
7万
查看次数

使用Spring Boot和Spock进行集成测试

@IntegrationTest使用Spock 运行集成测试(例如)的最佳方法是什么?我想引导整个Spring Boot应用程序并执行一些HTTP调用来测试整个功能.

我可以使用JUnit(首先运行app然后执行测试):

@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = MyServer.class)
@WebAppConfiguration
@IntegrationTest
class MyTest {
   RestTemplate template = new TestRestTemplate();

   @Test
   public void testDataRoutingWebSocketToHttp() {
      def a = template.getForEntity("http://localhost:8080", String.class)
      println a
   }
}
Run Code Online (Sandbox Code Playgroud)

但是使用Spock,应用程序无法启动:

@SpringApplicationConfiguration(classes = MyServer.class)
@WebAppConfiguration
@IntegrationTest
class MyTestSpec extends Specification {

   RestTemplate template = new TestRestTemplate();

   def "Do my test"() {
      setup:
      def a = template.getForEntity("http://localhost:8080", String.class)

      expect:
      println a
   }
}
Run Code Online (Sandbox Code Playgroud)

当然,对于Spock,我在Gradle构建文件中指定了正确的依赖项:

...
dependencies {
   testCompile 'org.spockframework:spock-core:0.7-groovy-2.0'
   testCompile 'org.spockframework:spock-spring:0.7-groovy-2.0'
}
...
Run Code Online (Sandbox Code Playgroud)

我错过了什么吗?

java groovy gradle spock spring-boot

36
推荐指数
3
解决办法
3万
查看次数

标签 统计

spring-boot ×2

embedded-tomcat-7 ×1

gradle ×1

groovy ×1

java ×1

port ×1

spock ×1

spring ×1