java - Spring 4 TestNG +@Autowired

标签 java spring spring-mvc junit testng

com.conf中有一个类

@Configuration
public class WebConfTest {

    @Autowired
private Environment environment;

}

并在 com.service 中进行单元测试

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = { WebConfTest.class })
public class DMSServiceImplTest {
    @Autowired
    WebConfTest webConfTest;

    @Test
    public void testConnect() throws Exception {

    }

}

测试依赖:

<dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>${springframework.version}</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.testng</groupId>
            <artifactId>testng</artifactId>
            <version>${testng.version}</version>
            <scope>test</scope>
        </dependency>

在 IDEA 中 bean 之间的导航工作。但是如果我运行测试,WebConfTest== null。

怎么了?

谢谢。

最佳答案

@RunWith 用于 junit runner。

如果你想用 TestNG 运行测试,你需要扩展 AbstractTestNGSpringContextTests

http://docs.spring.io/spring/docs/current/spring-framework-reference/html/integration-testing.html#testcontext-support-classes-testng

关于java - Spring 4 TestNG +@Autowired,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37251408/

相关文章:

spring - 在spring mvc中将多个参数从 View 传递给 Controller

validation - @Valid 注释可以在 spring mvc 中递归地检查字段吗?

java - Brixton Eureka/ConfigServer 与 Eureka/Config Angel 客户端的兼容性?

java - 电子商务应用程序设计(例如 : Amazon/ebay/flipkart ): Concurrency issues handling

java - 是否可以在单元测试期间禁用 Spring 的 @Async?

java - 如何在 Spring Boot 1 应用程序中查找未使用的属性

java - 如何在 Java 中绘制二维图形?

java - 尝试 setOnItemSelectedListener(this); 时应用程序崩溃

java - 通过后台删除hybris中的动态属性

java - 将 Spring Boot war 文件部署到 Tomcat 结果为 404,本地主机工作正常