spring-boot:应用程序加载但测试失败

时间:2015-06-30 21:18:24

标签: spring spring-boot spring-test

使用Spring Boot时,我遇到了相当奇怪的事情。让我们来吧。 我有一个应用程序,当从spring-boot:run运行时,加载完全正常,我可以使用我的服务器。但是,如果我尝试运行测试(通过从IntelliJ启动测试或通过surefire插件),则无法加载上下文。

问题在于此类(仅显示相关部分):

@RestController
@RequestMapping(
  value = "/sa/revisions/"
)
@SuppressWarnings("unchecked")
class RevisionController {
  @Autowired
  // cant autowire this field
  private RepositoryEntityLinks   repositoryEntityLinks = null;
  /* omitted */
}

这是我的主要课程:

@EnableAsync
@EnableCaching
@EnableAutoConfiguration
@EnableConfigurationProperties
@Import({
  SecurityConfiguration.class,
  DataConfiguration.class,
  RestConfiguration.class
})
public class SpringAtomApplication {
  @Autowired
  private DataLoaderManager dataLoaderManager = null;

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

  @Bean
  public CacheManager cacheManager() {
    final GuavaCacheManager manager = new GuavaCacheManager();
    manager.setAllowNullValues(false);
    return manager;
  }

  @PostConstruct
  private void doPostConstruct() {
    this.dataLoaderManager.doLoad();
  }
}

正如我所说,应用程序在正常运行时没有问题加载,但是当涉及到这个简单的测试时,一切都崩溃了:

@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = SpringAtomApplication.class)
public class SpringAtomApplicationTests {
  @Test
  public void contextLoads() {
  }
}

感谢任何建议,因为我喜欢从测试开始。

1 个答案:

答案 0 :(得分:-1)

您应该在测试类中设置 queryConfig = { type : 'defect', key : 'defects', query: '(((c_DeliveryVersionIntroduced contains "Version 1.1") AND (State != Closed)) AND (CreationDate = today-3))', fetch: 'Name,State,Severity,FormattedID,Project,ObjectID' }; var rallyDataSource; rallyDataSource = new rally.sdk.data.RallyDataSource('__WORKSPACE_OID__', '__PROJECT_OID__', '__PROJECT_SCOPING_UP__', '__PROJECT_SCOPING_DOWN__'); rallyDataSource.findAll(queryConfig, processResults); }

SpringApplicationContextLoader

使用它可以测试非Web功能(如存储库或服务)或启动完全配置的嵌入式servlet容器并使用MockMvc运行测试。

参考:http://docs.spring.io/spring-boot/docs/current/api/org/springframework/boot/test/SpringApplicationContextLoader.html