7
मेरे पास ConfigurationProperties
कक्षा है और junit
का उपयोग करके इसका परीक्षण करना चाहते हैं। लेकिन वस्तु हमेशा null
है। निम्नलिखित कोड में क्या गुम हो सकता है?ज्यूनिट के साथ वसंत में कॉन्फ़िगरेशनप्रॉपर्टीज़ का परीक्षण कैसे करें?
@EnableAutoConfiguration
@ComponentScan
@EnableConfigurationProperties(MyProperties.class)
public class AppConfig {
}
@Service
public class MyService {
@Autowired
private MyProperties props;
public void run() {
props.getName();
}
}
@Component
@ConfigurationProperties(prefix = "my")
public class MyProperties {
private String name;
//getter,setter
}
application.properties:
my.name=test
परीक्षण:
@Configuration
@ComponentScan(basePackageClasses = {MyService.class, MyProperties.class},
includeFilters = @ComponentScan.Filter(value = {MyService.class, MyProperties.class},
type = FilterType.ASSIGNABLE_TYPE),
lazyInit = true
)
@PropertySources(
@PropertySource("application.properties")
)
class AppTest {
@Bean
public static PropertySourcesPlaceholderConfigurer propertiesResolver() {
return new PropertySourcesPlaceholderConfigurer();
}
}
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = ApplicationConfigTest.class)
public class MyTest extends AbstractJUnit4SpringContextTests {
@Autowired
private MyService service;
@Test
public void testService() {
service.run();
}
}