0

With SpringBoot i can easily run the same Application as different instances with different configurations, for example:

@SpringBootApplication
@ComponentScan({"..."})
public class Application {

    public static void main(String[] args) {
        start(Application.class)
            .properties("notification.sender.app.name=SomeApp", "notification.this.app.name =AnotherApp", "server.port=${first.port:9010}").run(args);
        start(Application.class)
            .properties("first.app.name=AnotherApp", "second.app.name =SomeApp", "server.port=${second.port:9020}").run(args);
    }

    private static SpringApplicationBuilder start(Class<?>... sources) {
        return new SpringApplicationBuilder(sources).bannerMode(Mode.OFF);
    }
}

Which is terrific, specially for testing inter application communication stuff.

I am trying to achieve now the same with @SpringBootTest to run Unit Tests against the running Applications instances.

Probably easy, but I haven't got it.

HDJEMAI
  • 9,436
  • 46
  • 67
  • 93

1 Answers1

1

The way I achieve this in Junit Tests with @ContextConfiguration and SpringRunner.class as Test runner:

@Before
public void startUp() {
    someAppContext = new SpringApplicationBuilder(Application.class)
            .properties("notification.app.name=SomeApp", "server.port=" + someAppPort).run();
    anotherAppContext = new SpringApplicationBuilder(Application.class)
            .properties("notification.app.name=AnotherApp", "server.port=" + anotherAppPort).run();
}

where someAppPort and anotherAppPort is configured with @Value.

Nicomedes E.
  • 1,326
  • 5
  • 18
  • 27
  • can you please explain this solution? – afe Jul 27 '21 at 09:07
  • @afe You need a Spring Boot Application.class , annotated as you need it. In a Test Class , which you run with @RunWith(SpringRunner.class), you a setup method startup() , which runs the same Spring Boot Application.class twice on two different ports. – Christoph Henrici Jul 27 '21 at 15:06
  • thanks for the details. What I always need in java is the name of the packages you import and the gradle/maven repository. – afe Jul 28 '21 at 15:45