【问题标题】:Spring cucumber doesn't inject serviceSpring Cucumber 不注入服务
【发布时间】:2016-06-28 08:02:36
【问题描述】:

我的主应用程序使用 springboot 运行,并且我已经实现了一个服务,服务器运行良好,所有单元测试都通过了,并且服务运行良好(所有邮递员请求都成功并且所有测试都通过了)。试图转向黄瓜测试。

@Inject
private servicesImpl serviceImpl;

总是为空,我不明白为什么 autowired/inject 不能真正注入服务。 这些是类上的注释:

@RunWith(Cucumber.class)    
@WebIntegrationTest
@SpringApplicationConfiguration(classes = UniversityApplication.class)

这些是我的黄瓜依赖项:

compile group: 'info.cukes', name: 'cucumber-spring', version: '1.2.4'
compile group: 'info.cukes', name: 'cucumber-junit', version: '1.2.4'

【问题讨论】:

    标签: java spring cucumber


    【解决方案1】:

    创建一个黄瓜测试类:

    @RunWith(Cucumber.class)
    @CucumberOptions(features = "src/test/resorces")
    public class CucumberTest {
    }
    

    步骤定义类为:

    @ContextConfiguration(classes = TestApplication.class, 
                          loader = SpringApplicationContextLoader.class)
    @RunWith(SpringJUnit4ClassRunner.class)
    @TestPropertySource(locations = "classpath:test.properties")
    @WebAppConfiguration
    @IntegrationTest
    public class StepsDefinitions {
    
      @Inject
      Services servicesImpl;
    }
    

    spring boot 应用为:

    @SpringBootApplication
    public class TestApplication {
    
      @Bean
      Service service() {
        return new ServiceImpl();
      }
    }
    

    【讨论】:

    • 试过了,没用,@Inject/@Autowired 服务仍然为空
    • 忘记在 StepsDefinitions 上添加 @IntegrationTest
    猜你喜欢
    • 2017-05-11
    • 1970-01-01
    • 1970-01-01
    • 2016-08-16
    • 1970-01-01
    • 1970-01-01
    • 2017-10-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多