SpringBoot Junit测试main方法
SpringBoot Junit testing main method
我对 spring 启动主要方法进行了以下测试。
测试尝试启动应用程序 2 次,这是预期的。
第一次启动应用程序时,它使用 Mock 对象,第二次启动应用程序时,它调用实际的 bean。
我有 ReferenceDataService
有 @PostConstract
方法调用,它对我在测试中不需要的其他应用程序进行 rest 调用。
另一件事是 MqConfiguration
尝试连接到 IBM 队列,我也希望在我的测试中避免这种情况。
请注意,尽管我在测试中添加了 @ComponentScan(excludeFilters...
class,但它并不排除它。
在这种情况下,如何为我的主要方法编写测试?
@ActiveProfiles(profiles = {"test"})
@RunWith(SpringRunner.class)
@SpringBootTest(classes = MainApplication.class, webEnvironment = WebEnvironment.RANDOM_PORT, properties = {
"camel.springboot.java-routes-include-pattern=**/NONE*"})
@EnableAutoConfiguration(exclude = {DataSourceAutoConfiguration.class, DataSourceTransactionManagerAutoConfiguration.class, SecurityAutoConfiguration.class})
@DirtiesContext(classMode = ClassMode.AFTER_EACH_TEST_METHOD)
@ComponentScan(excludeFilters = {@ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE, value = {MqConfiguration.class, ReferenceDataCache.class})})
public class MainApplicationTest {
@MockBean
private MqService mqService;
@MockBean
private ReferenceDataService referenceDataService;
@SpyBean
private ReferenceDataCache cacheSpy;
@Test
public void test() {
Mockito.when(referenceDataService.getCurrencies()).thenReturn(new HashMap<>());
Mockito.when(referenceDataService.getFrequencies()).thenReturn(null);
Mockito.when(referenceDataService.getDayCountTypes()).thenReturn(null);
Mockito.when(referenceDataService.getBusinessDayConverntions()).thenReturn(null);
Mockito.when(referenceDataService.getRateDefinations()).thenReturn(null);
Mockito.when(referenceDataService.getBusinessCalendar()).thenReturn(null);
Mockito.when(referenceDataService.getFinancingTypes()).thenReturn(null);
Mockito.when(referenceDataService.getStaffs()).thenReturn(null);
MainApplication.main(new String[]{});
}
}
MainApplication.java (The class to be tested)
@SpringBootApplication
@EnableJms
@EnableCaching
@AutoConfigureBefore(JmsAutoConfiguration.class)
public class MainApplication {
private static final Logger logger = LoggerFactory.getLogger(MainApplication.class);
public static void main(String[] args) {
SpringApplication.run(MainApplication.class, args);
}
}
可以将它分成两个单独的测试部分,因为我们应该努力在每个测试中测试一个功能(单一职责原则)。您可以像下面这样对您的测试进行建模:
@Test
public void applicationContextLoadedTest(){
}
@Test
public void applicationStartTest() {
//you can add your mocks as per your required dependencies and requirements
MainApplication.main(new String[] {});
}
或者,如果您被允许使用 PowerMockito,则以下 link 为您提供了一个验证静态调用的工作示例。PowerMockito - SpringBoot test
我对 spring 启动主要方法进行了以下测试。
测试尝试启动应用程序 2 次,这是预期的。
第一次启动应用程序时,它使用 Mock 对象,第二次启动应用程序时,它调用实际的 bean。
我有 ReferenceDataService
有 @PostConstract
方法调用,它对我在测试中不需要的其他应用程序进行 rest 调用。
另一件事是 MqConfiguration
尝试连接到 IBM 队列,我也希望在我的测试中避免这种情况。
请注意,尽管我在测试中添加了 @ComponentScan(excludeFilters...
class,但它并不排除它。
在这种情况下,如何为我的主要方法编写测试?
@ActiveProfiles(profiles = {"test"})
@RunWith(SpringRunner.class)
@SpringBootTest(classes = MainApplication.class, webEnvironment = WebEnvironment.RANDOM_PORT, properties = {
"camel.springboot.java-routes-include-pattern=**/NONE*"})
@EnableAutoConfiguration(exclude = {DataSourceAutoConfiguration.class, DataSourceTransactionManagerAutoConfiguration.class, SecurityAutoConfiguration.class})
@DirtiesContext(classMode = ClassMode.AFTER_EACH_TEST_METHOD)
@ComponentScan(excludeFilters = {@ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE, value = {MqConfiguration.class, ReferenceDataCache.class})})
public class MainApplicationTest {
@MockBean
private MqService mqService;
@MockBean
private ReferenceDataService referenceDataService;
@SpyBean
private ReferenceDataCache cacheSpy;
@Test
public void test() {
Mockito.when(referenceDataService.getCurrencies()).thenReturn(new HashMap<>());
Mockito.when(referenceDataService.getFrequencies()).thenReturn(null);
Mockito.when(referenceDataService.getDayCountTypes()).thenReturn(null);
Mockito.when(referenceDataService.getBusinessDayConverntions()).thenReturn(null);
Mockito.when(referenceDataService.getRateDefinations()).thenReturn(null);
Mockito.when(referenceDataService.getBusinessCalendar()).thenReturn(null);
Mockito.when(referenceDataService.getFinancingTypes()).thenReturn(null);
Mockito.when(referenceDataService.getStaffs()).thenReturn(null);
MainApplication.main(new String[]{});
}
}
MainApplication.java (The class to be tested)
@SpringBootApplication
@EnableJms
@EnableCaching
@AutoConfigureBefore(JmsAutoConfiguration.class)
public class MainApplication {
private static final Logger logger = LoggerFactory.getLogger(MainApplication.class);
public static void main(String[] args) {
SpringApplication.run(MainApplication.class, args);
}
}
可以将它分成两个单独的测试部分,因为我们应该努力在每个测试中测试一个功能(单一职责原则)。您可以像下面这样对您的测试进行建模:
@Test
public void applicationContextLoadedTest(){
}
@Test
public void applicationStartTest() {
//you can add your mocks as per your required dependencies and requirements
MainApplication.main(new String[] {});
}
或者,如果您被允许使用 PowerMockito,则以下 link 为您提供了一个验证静态调用的工作示例。PowerMockito - SpringBoot test