How to test main class of Spring-boot application

后端 未结 9 1857
心在旅途
心在旅途 2020-12-03 00:18

I have a spring-boot application where my @SpringBootApplication starter class looks like a standard one. So I created many tests for all my functi

9条回答
  •  醉梦人生
    2020-12-03 01:15

    This simple mock test for SpringApplication does not invoke any methods but just tests the starter app. [uses PowerMockRunner.class]

    import org.junit.Test;
    import org.junit.runner.RunWith;
    import org.powermock.api.mockito.PowerMockito;
    import org.powermock.core.classloader.annotations.PowerMockIgnore;
    import org.powermock.core.classloader.annotations.PrepareForTest;
    import org.powermock.modules.junit4.PowerMockRunner;
    import org.springframework.boot.SpringApplication;
    
    @RunWith(PowerMockRunner.class)
    @PowerMockIgnore({"com.sun.org.apache.xerces.*", "javax.xml.*", "org.xml.*", "javax.management.*"})
    public class JobsAppStarterTest {
    
        @Test
        @PrepareForTest(SpringApplication.class)
        public void testSpringStartUp() {
            PowerMockito.mockStatic(SpringApplication.class);
            SpringApplication.run(JobsAppStarter.class, new String[] {"args"});
            JobsAppStarter.main(new String[] {"args"});
        }
    }
    

提交回复
热议问题