@godmodeon08
амбициозный разраб

Как протестировать spring @component?

Привет!
Условно говоря, есть класс:

@Component
public class TelegramBot extends TelegramLongPollingBot implements InitializingBean {

    private static final org.slf4j.Logger log = LoggerFactory.getLogger(TelegramBot.class);
    private static final SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");

    @Autowired
    private TelegramDao telegramDao;

    @Value("${spring.botusername}")
    private String botUserName;

    @Value("${spring.bottoken}")
    private String botToken;
 


    public void sendMessage(Chat chat, String message) {
    
    }
 
}


Я хочу протестировать вызов метода sendMessage.
Написал тест:

@RunWith(SpringJUnit4ClassRunner.class)

public class TelegramTest {

    @Autowired
    @Qualifier("bot")
    private TelegramBot telegramBot;




    @Test
    public void test1(){
         telegramBot.sendMessage(null,null);
    }


}


Получаю ошибку:

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'telegram.TelegramTest': Unsatisfied dependency expressed through field 'telegramBot'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'TelegramBot' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true), @org.springframework.beans.factory.annotation.Qualifier(value=bot)}


Что я делаю не так?
  • Вопрос задан
  • 378 просмотров
Решения вопроса 1
@godmodeon08 Автор вопроса
амбициозный разраб
Не, надо было так тест сделать:
@RunWith(SpringRunner.class)
@SpringBootTest(classes={SpringBootApp.class})
public class TelegramTest {

    @Autowired
    private TelegramBot telegramBot;




    @Test
    public void test1(){
         telegramBot.sendMessage(null,null);
    }


}

Те в заголовке класса обновил аннотации на:
@RunWith(SpringRunner.class)
@SpringBootTest(classes={SpringBootApp.class})


И все заработало:)
Ответ написан
Комментировать
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы