@Autowire的Spring Boot测试问题

时间:2019-05-27 08:21:08

标签: java spring spring-boot testing

我是Spring Boot测试的新手。尝试测试AwsTestsExtractor类下面的某些方法时,与加载应用程序上下文有关的错误。

错误消息:

  

com.silvio.me.Prototype中的awsTestsExtractor字段需要一个Bean   键入找不到的“ com.silvio.me.AwsTestsExtractor”。

     

注入点具有以下注释:     -@ 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));
    }
}

我想我犯了一些根本性的错误,任何帮助都值得赞赏。

0 个答案:

没有答案