我有一个需要测试的服务,它已经注入了存储库
@Service
public class MyService {
@Inject
private MyRepo myRepo;
public someFunctionToTest(){
// body...
}
}
然而我无法弄清楚如何为服务编写正确的junit测试
我试过以下:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = { Application.class })
@Configuration
public class NumberGeneratorServiceTest {
@Inject
private MyService myService;
@Inject
private MyRepository MyRepository;
@Test
public void test() {
//do something
}
}
在这种情况下我得到错误:Failed to load ApplicationContext
Caused by: org.springframework.beans.factory.BeanCurrentlyInCreationException: Error creating bean with name 'getMyRepository': Requested bean is currently in creation: Is there an unresolvable circular reference?
使用注射测试服务的正确方法是什么?
编辑:MyRepository
@Repository
@AllArgsConstructor
@NoArgsConstructor
public class MyRepositoryImpl implements MyRepository {
@Inject
private MyJPARepository entityRepository;
//some methods
}