0

I have a custom library (a starter) created with SpringBoot. So, it doesn't have main method or @SpringBootApplication annotated class.

I want to test this starter functionality with JUnit. I have created a class in src/test (TestApplication) to be a launcher.

@SpringBootTest
@ContextConfiguration(classes = TestApplication.class)
@RunWith(SpringRunner.class)
public class SettingsTest {
    @Autowired
    private MyService myService;
    @Test
    public void testSettingsLoad(){
      //do smth
    }
}

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

However, TestApplication main is not invoked and application context is not raised correctly. I've tryied without @RunWith annotation (no Spring context at all) and with adding class directly to @SpringBootTest annotation (also ignored).

I've read this post, but it wasn't really helpful.

Any ideas appreciated.

ThisaruG
  • 3,222
  • 7
  • 38
  • 60
Ermintar
  • 1,322
  • 3
  • 22
  • 39

1 Answers1

1

A starter is an auto-configuration. When i have written starters, i don't test the spring-boot application that uses the starter, i test the starter itself and this is pretty well documented in section 49.4 Testing your Auto-configuration

private final ApplicationContextRunner contextRunner = new ApplicationContextRunner()
    .withConfiguration(AutoConfigurations.of(UserServiceAutoConfiguration.class));

@Test
public void defaultServiceBacksOff() {
    this.contextRunner.withUserConfiguration(UserConfiguration.class)
        .run((context) -> {
            assertThat(context).hasSingleBean(UserService.class);
            assertThat(context.getBean(UserService.class))
            .isSameAs(context.getBean(UserConfiguration.class).myUserService());
    });
}

So what you do is that you create a ApplicationRunnerContext and then load you entryclass from your starter and then you assert in that context what has been loaded.

Toerktumlare
  • 12,548
  • 3
  • 35
  • 54