Spring boot 2.2 ошибка при попытке выполнить тест в сервисном слое - PullRequest
0 голосов
/ 06 апреля 2020

в день я пытаюсь выполнить тест в сервисном слое, используя Spring boot 2.2. Но при выполнении этого теста зависимость значения bean-компонента равна нулю:

Зависимость для теста:

<dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
        <exclusions>
            <exclusion>
                <groupId>org.junit.vintage</groupId>
                <artifactId>junit-vintage-engine</artifactId>
            </exclusion>
        </exclusions>
    </dependency>

Служба bean-компонента для проверки:

@Service("exampleService")
public class ExampleServiceImpl  implements ExampleService{

    private static final Log  LOGGER = LogFactory.getLog(ExampleServiceImpl.class);

     @Autowired
     private ConfigDepartment appProperties; //here is null when execute test

    @Override
    public String returnMensaje() {
        LOGGER.info(appProperties.getDev());

        String texto = "Hola ";
        return texto+ appProperties.getDev();
    }

}

Выполнен только нулевой бин конфигурации test:

@Configuration
@ConfigurationProperties(prefix = "deparment")
public class ConfigDepartment {

    private String dev;

    public String getDev() {
        return dev;
    }

    public void setDev(String dev) {
        this.dev = dev;
    }
}

Теперь это мой тест для запуска:

@SpringJUnitConfig(classes = {
        ExampleServiceTests.class, ExampleService.class,
        ExampleServiceImpl.class, ConfigDepartment.class})
@WebAppConfiguration
public class ExampleServiceTests {

    private static final String expected = "hello Person";

    @Autowired
    @Qualifier("exampletService")
    private ExampleService exampleService;

     @Test
        void returnMensaje() {
         System.out.println("Test bean");
         assertEquals(expected, exampleService.returnMensaje());
        }
}

Служба не может внедрить или инициализировать ConfigDepartment (ExampleWalmartServiceImpl)

Знаете ли вы, как я мог получить бин config для правильной инициализации при запуске теста? Попробуйте EnabledConfiguration, но это не сработало.

Обновление:

Решение это было:

@SpringJUnitConfig(classes = {
        ExampleServiceTests.class, ExampleService.class,
        ExampleServiceImpl.class, ConfigDepartment.class, SpringBootContextLoader.class})
@EnableAutoConfiguration
@ExtendWith(SpringExtension.class)
@SpringBootTest

После добавления эта аннотация в тестовом классе (ExampleServiceTests), ошибка решена.

С уважением

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...