Java 无法将服务注入到Spring测试中
Java 无法将服务注入到Spring测试中,java,spring,testing,junit,jhipster,Java,Spring,Testing,Junit,Jhipster,financialReportService在REST控制器中为空,表示无法注入 测试: @RunWith(SpringRunner.class) @SpringBootTest(classes = SnapshotfindocApp.class) public class FindocResourceIntTest { @Inject private FinancialReportService financialReportService; @Before public v
financialReportService
在REST控制器中为空,表示无法注入
测试:
@RunWith(SpringRunner.class)
@SpringBootTest(classes = SnapshotfindocApp.class)
public class FindocResourceIntTest {
@Inject
private FinancialReportService financialReportService;
@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);
}
}
@Service
@Transactional
public class FinancialReportService {
private final Logger log = LoggerFactory.getLogger(FinancialReportService.class);
@Inject
private FinancialReportDAO financialReportDAO;
public List<Findoc> getFinancialReports(){
return financialReportDAO.getFinancialReports();
}
}
@RunWith(SpringRunner.class)
@SpringBootTest(类=SnapshotfindocApp.class)
公共类FindocResourceIntTest{
@注入
私人财务报告服务财务报告服务;
@以前
公共作废设置(){
initMocks(this);
FindocResource FindocResource=新的FindocResource();
ReflectionTestUtils.setField(findocResource,“findocRepository”,findocRepository);
this.restFindocMockMvc=MockMvcBuilders.standaloneSetup(findocResource)
.SetCustomArgumentResolver(pageableArgumentResolver)
.setMessageConverters(jacksonMessageConverter).build();
}
@试验
@交易的
public void getFinancialRecords()引发异常{
//获取所有财务报告
restFindocMockMvc.perform(get(“/api/financial reports”))
.andExpect(status().isOk());
List finReports=financialReportService.getFinancialReports();
对于(Findoc fr:finReports){
资产(fr.getNo_months())。介于(12,18)之间;
LocalDate documentTimeSpanLimit=LocalDate.now().minusMonths(18);
资产(fr.getFinancial_date()).isAfterOrEqualTo(documentTimeSpanLimit);
}
}
服务:
@RunWith(SpringRunner.class)
@SpringBootTest(classes = SnapshotfindocApp.class)
public class FindocResourceIntTest {
@Inject
private FinancialReportService financialReportService;
@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);
}
}
@Service
@Transactional
public class FinancialReportService {
private final Logger log = LoggerFactory.getLogger(FinancialReportService.class);
@Inject
private FinancialReportDAO financialReportDAO;
public List<Findoc> getFinancialReports(){
return financialReportDAO.getFinancialReports();
}
}
@服务
@交易的
公共类财务报告服务{
私有最终记录器log=LoggerFactory.getLogger(FinancialReportService.class);
@注入
私人财务报告财务报告;
公共列表getFinancialReports(){
返回financialReportDAO.getFinancialReports();
}
}
控制器:
@GetMapping("/financial-reports")
@Timed
public List<Findoc> getFinancialReports() {
log.debug("REST request to get financial records");
return financialReportService.getFinancialReports(); // financialReportService is null
}
@GetMapping(“/financial reports”)
@定时
公共列表getFinancialReports(){
log.debug(“获取财务记录的REST请求”);
返回financialReportService.getFinancialReports();//financialReportService为空
}
更新:
@RunWith(SpringRunner.class)
@SpringBootTest(classes = SnapshotfindocApp.class)
public class FindocResourceIntTest {
@Inject
private FinancialReportService financialReportService;
@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);
}
}
@Service
@Transactional
public class FinancialReportService {
private final Logger log = LoggerFactory.getLogger(FinancialReportService.class);
@Inject
private FinancialReportDAO financialReportDAO;
public List<Findoc> getFinancialReports(){
return financialReportDAO.getFinancialReports();
}
}
应用程序由JHipster生成。然后添加了和DAO文件以支持对H2的自定义数据库查询。在
@Inject
关闭服务后,您还需要在setup()
方法中设置字段。添加以下行应该可以解决您的问题
ReflectionTestUtils.setField(findocResource, "financialReportService", financialReportService);
另一方面,测试的以下部分看起来很奇怪。您将获取两次财务报告。此文件是FindocResourceIntTest,因此我将删除对financialReportService
的任何直接调用
// Get all the financial-reports
restFindocMockMvc.perform(get("/api/financial-reports"))
.andExpect(status().isOk());
List<Findoc> finReports = financialReportService.getFinancialReports();
//获取所有财务报告
restFindocMockMvc.perform(get(“/api/financial reports”))
.andExpect(status().isOk());
List finReports=financialReportService.getFinancialReports();