dimanche 29 janvier 2017

Cannot inject service into Spring test

financialReportService is null that denotes it fails to inject. Test:

@RunWith(SpringRunner.class)
@SpringBootTest(classes = SnapshotfindocApp.class)
public class FindocResourceIntTest {
@Inject
    private FinancialReportService financialReportService; // todo::  null
@Before
    public void setup() {
        MockitoAnnotations.initMocks(this);
        FindocResource findocResource = new FindocResource();
        ReflectionTestUtils.setField(findocResource, "findocRepository", findocRepository);
        this.restFindocMockMvc = MockMvcBuilders.standaloneSetup(findocResource)
            .setCustomArgumentResolvers(pageableArgumentResolver)
            .setMessageConverters(jacksonMessageConverter).build();
    }


@Test
    @Transactional
    public void getFinancialRecords() throws Exception {

        // Get all the financial-reports
        restFindocMockMvc.perform(get("/api/financial-reports"))
            .andExpect(status().isOk());
        List<Findoc> finReports = financialReportService.getFinancialReports();
        for (Findoc fr : finReports) {
            assertThat(fr.getNo_months()).isBetween(12, 18);
            LocalDate documentTimeSpanLimit = LocalDate.now().minusMonths(18);
            assertThat(fr.getFinancial_date()).isAfterOrEqualTo(documentTimeSpanLimit);
        }
    }

The service:

@Service
@Transactional
public class FinancialReportService {

    private final Logger log = LoggerFactory.getLogger(FinancialReportService.class);

    @Inject
    private FinancialReportDAO financialReportDAO;

    public List<Findoc> getFinancialReports(){
        return financialReportDAO.getFinancialReports();
    }

}

Aucun commentaire:

Enregistrer un commentaire