0
我試圖單元測試春天啓動的應用。作爲main
方法的一部分,我在調用服務Configuration
方法init
做一些數據的設置。 Configuration
服務在Description
服務自動裝配。當我試圖單元測試Description
服務,NPE作爲init
方法不Configuration
服務調用拋出。如何解決這個問題。春天開機單元的JUnit測試不加載配置
BatchService:
@Configuration
@EnableAutoConfiguration
@ComponentScan("com.test.poc")
public class BatchService {
public static void main(String[] args) {
ConfigurableApplicationContext context = null;
SpringApplication application = new SpringApplication(BatchService.class);
try{
context = application.run();
//Initialize
Configuration config = (Configuration)context.getBean("Configuration");
config.init();
}catch(Exception e){
e.printStackTrace();
}
}
}
ConfigurationService:
@Service
public class Configuration {
private ConfigValue value = null;
public void init(){
//
}
public String getValue(){
return value.getNameValue();
}
}
DescriptionService:
@Service
public class DescriptionService {
@Autowired Configuration config;
public List<String> getDescription(){
String value = config.getValue();
}
}
AbstractAutonomicsTest:
@ActiveProfiles("test")
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = BatchService.class)
@TestPropertySource(locations="classpath:application-test.properties")
public abstract class AbstractAutonomicsTest {
}
DescriptionClusteringServiceTest:
public class DescriptionClusteringServiceTest extends AbstractAutonomicsTest {
@Autowired DescriptionClusteringService descService;
@Test
public void testDescription(){
List<String> out = descService.getDescription();
}
}