0
package com.example.demo;

import org.springframework.boot.Banner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class DemoApplication {

    public static void main(String[] args) {

        System.out.println("Hello !");

        // SpringApplication.run(DemoApplication.class, args);

        SpringApplication application = new SpringApplication(DemoApplication.class);
        application.setBannerMode(Banner.Mode.OFF);
        application.run(args);
    }

}

And test

package com.example.demo;

import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;


@SpringBootTest(classes = DemoApplication.class, webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT)
public class DemoApplicationTests {

    @Test
    void contextLoads() {
    }

}

DemoApplicationTest seems to not use or be aware anythig about DemoApplication (@Spring BootApplication).

And I can't figure out if Maven project has multiple maven modules, how can they be included in test. Seem to me that Spring app does not even care about profiles defined in test.

Please point to relevant example or documentation: how to configure the test, so that it works like Spring Boot service where running on its own.

CommandLineRunner however gets executed also when running from test class:

@SpringBootApplication
public class DemoApplication implements CommandLineRunner {

    public static void main(String[] args) {

        System.out.println("Hello !");

        // SpringApplication.run(DemoApplication.class, args);

        SpringApplication application = new SpringApplication(DemoApplication.class);
        application.setBannerMode(Banner.Mode.OFF);
        application.run(args);
    }

    @Autowired
    ApplicationContext applicationContext;

    @Override
    public void run(String... args) throws Exception {
        String[] beans = applicationContext.getBeanDefinitionNames();
        Arrays.sort(beans);
        for (String bean : beans) {
            System.out.println(bean);
        }
        System.out.println(".............................");
    }
}

But with Spring-banner .

2

1 Answer 1

0

First, add @ExtendWith(SpringExtension.class) to your test class. Rest of the example, you can check here :

https://howtodoinjava.com/spring-boot2/testing/spring-boot-2-junit-5/

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.