Spring 4 TestNG + @Autowired

stu*_*ent 1 java junit testng spring spring-mvc

在包中有一个类 com.conf

@Configuration
public class WebConfTest {

    @Autowired
private Environment environment;

}
Run Code Online (Sandbox Code Playgroud)

和单元测试 com.service

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

    @Test
    public void testConnect() throws Exception {

    }

}
Run Code Online (Sandbox Code Playgroud)

测试依赖:

<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>
Run Code Online (Sandbox Code Playgroud)

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

怎么了?

谢谢.

juh*_*err 7

@RunWith 是为了junit runner.

如果要使用TestNG运行测试,则需要进行扩展AbstractTestNGSpringContextTests.

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