when i use spring-boot for no-web application, in the spring-boot-test ,"server.port" was be override by org.springframework.boot.test.context.SpringBootContextLoader.getInlinedProperties() properties.add("server.port=-1") . therefore on my unit test, i cannot start server.

application.properties:

server.port=12345
server.name=myserver`

MyServer.java:

@Component
public class MyServer implements Closeable {

    @Value("${server.port}")
    private int serverPort;

    @Value("${server.name}")
    private String serverName;

    private ServerSocket serverSocket;

    @PostConstruct
    public void init() {
        System.out.println("server.port:" + serverPort);
        System.out.println("server.name:" + serverName);
        // start my server on port "server.port"
        ....
    }

}

SpringBootDemoApplication.java:

@SpringBootApplication
public class SpringBootDemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(SpringBootDemoApplication.class, args);
    }

}

SpringBootDemoApplicationTest.java:

@RunWith(SpringRunner.class)
@SpringBootTest
public class SpringBootDemoApplicationTest {

    @Resource
    private MyServer myServer;

    @After
    public void tearDown() throws Exception {
        myServer.close();
    }

    @Test
    public void contextLoads() throws Exception {
        String message = MyClient.sendToServer();
        assertThat(message, startsWith("hello client!"));
    }


}

spring-boot-demo.zip

Comment From: philwebb

We might be able to improve the detection logic, but in the meantime you can try using the @SpringBootTest(webEnvironment=WebEnvironment.NONE)

Comment From: midstr

We might be able to improve the detection logic, but in the meantime you can try using the @SpringBootTest(webEnvironment=WebEnvironment.NONE)

It didn't work, and if I add @TestPropertySource(properties = "server.port=12345") is the my solution for now.

Comment From: snicoll

when i use spring-boot for no-web application, in the spring-boot-test ,"server.port" was be override by

@midstr what is your use of server.port? server.* is a reserved namespace for the embedded server and while we shouldn't set it to -1 with a non web application, it would nice if you had dedicated properties for something that's not related to the standard embedded web server. Can you please clarify the use case?

Comment From: midstr

when i use spring-boot for no-web application, in the spring-boot-test ,"server.port" was be override by

@midstr what is your use of server.port? server.* is a reserved namespace for the embedded server and while we shouldn't set it to -1 with a non web application, it would nice if you had dedicated properties for something that's not related to the standard embedded web server. Can you please clarify the use case?

in our case ,we use spring-boot + netty for customize server (and customize protocol, not a web server). Common properties like server.idserver.port server.name we use

Comment From: mindhaq

I have this issue as well, and I am not specifying server.port manually.

I'm using the workaround for now.

Comment From: nkjackzhang

when i use spring-boot for no-web application, in the spring-boot-test ,"server.port" was be override by

@midstr what is your use of server.port? server.* is a reserved namespace for the embedded server and while we shouldn't set it to -1 with a non web application, it would nice if you had dedicated properties for something that's not related to the standard embedded web server. Can you please clarify the use case?

in our case ,we use spring-boot + netty for customize server (and customize protocol, not a web server). Common properties like server.idserver.port server.name we use

There is another judgement using hasCustomServerPort(), if you config custom server port, this method will return true, and server port will not be overridden.

Comment From: snicoll

Looking at the PR and the current code, I don't really understand why we set server.port to -1 at all when there isn't an embedded environment. Flagging for team attention to see if someone remembers.

Comment From: wilkinsona

The only thing that I can think of is that a port < 0 is used to disable auto-start of the container and setting the property is a left-over from before the web environment stuff was reworked?