java - @Autowire 的 Spring Boot 测试问题
问题描述
我是春季启动测试的新手。当我尝试测试 AwsTestsExtractor 类下面的某些方法时,出现与加载应用程序上下文相关的错误。
错误信息:
com.silvio.me.Prototype 中的字段 awsTestsExtractor 需要找不到类型为“com.silvio.me.AwsTestsExtractor”的 bean。
注入点有以下注解: - @org.springframework.beans.factory.annotation.Autowired(required=true)
行动:
考虑在您的配置中定义“com.silvio.me.AwsTestsExtractor”类型的 bean。
代码:
@Component
public class AwsTestsExtractor extends TestsExtractor {
@Autowired
private ExaminationRepository examinationRepository;
public AwsTestsExtractor() { }
...
...
private String getTestDbRef(String description) {
ArrayList<Examination> strArr = new ArrayList<>(examinationRepository.customQuery(description));
if(strArr.size() > 0)
return strArr.get(0).getName();
else
return null;
}
}
@SpringBootApplication
public class Prototype implements CommandLineRunner {
@Autowired
private AwsTestsExtractor awsTestsExtractor;
public static void main(String[] args) {
SpringApplication.run(Prototype.class, args);
}
@Override
public void run(String... args) throws Exception {
String document="src/main/resources/test2.jpg";
awsTestsExtractor.extract(document);
}
catch (Exception ex) {
System.out.println(ex.getMessage());
}
}
}
@RunWith(SpringRunner.class)
@TestPropertySource(locations = "classpath:application-integrationtest.properties")
@DataMongoTest
public class AwsTestsExtractorTest {
@Autowired
private MongoTemplate mongoTemplate;
@Autowired
private AwsTestsExtractor awsTestsExtractor;
@Before
public void setUp() {
mongoTemplate.save(new Examination("terefere"));
}
@Test
public void getTestDbRefTest() {
assertTrue(ReflectionTestUtils.invokeMethod(awsTestsExtractor, "getTestDbRef","terefere" ).equals(true));
}
}
我想我犯了一些根本性的错误,任何帮助表示赞赏。