У меня есть тестовый класс в Junit4, который должен использовать NutrientListService.
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = ApplicationContext.class)
public class CalculationTests {
private NutrientListService nutrientService;
@Test
public void someTest()
Result re = Calculator.calculate(response, nutrientService)
}
Я получаю нулевой нутриентный сервис, поэтому попытался настроить ApplicationContext.
@Configuration
@ComponentScan("myservice")
@ComponentScan("myrepository")
public class ApplicationContext {
@Autowired
NutrientListService nutrientService;
}
Однако я получаю
Error creating bean with name 'nutrientListService': Unsatisfied dependency expressed through field 'nutrientListRepository'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'repositories.NutrientListRepository' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
Это услуга:
@Service
@Component
public class NutrientListService {
@Autowired
private NutrientListRepository repo;
}
И хранилище:
@Repository
public interface NutrientListRepository extends MongoRepository<MyClass, String> {
MyClass findByID(String ID);
}
Есть идеи, как правильно подключить услугу? Мне нужно передать его для расчета, так как это один из параметров. Нужно ли использовать класс контекста приложения или application-context.xml (который я не смог найти)? Каков был бы наименее неясный способ сделать это? Я благодарю вас.