lundi 27 mai 2019

Spring boot testing problem with @Autowire

I am new to spring boot testing. I have error related to loading application context when I am trying to test some methods of below AwsTestsExtractor class.

Error message:

Field awsTestsExtractor in com.silvio.me.Prototype required a bean of type 'com.silvio.me.AwsTestsExtractor' that could not be found.

The injection point has the following annotations: - @org.springframework.beans.factory.annotation.Autowired(required=true)

Action:

Consider defining a bean of type 'com.silvio.me.AwsTestsExtractor' in your configuration.

Code:

@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));
    }
}

I suppose I make some fundamental mistake, any help appreciated.

Aucun commentaire:

Enregistrer un commentaire