简体   繁体   中英

Spring 4 TestNG +@Autowired

Have a class in the package com.conf

@Configuration
public class WebConfTest {

    @Autowired
private Environment environment;

}

and unit test into com.service

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

    @Test
    public void testConnect() throws Exception {

    }

}

test dependency :

<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>

In the IDEA navigation between beans work. But WebConfTest == null if I run test.

What is wrong?

Thanks.

@RunWith is for junit runner.

If you want to run tests with TestNG, you need to extends AbstractTestNGSpringContextTests .

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM