Spring ContextConfiguration似乎没有为TestNg testcase加载我的bean

Spring ContextConfiguration似乎没有为TestNg testcase加载我的bean,spring,maven,testng,Spring,Maven,Testng,我是TestNg新手。我正在尝试测试一个示例hello world程序。 以下是我的项目结构 源程序包 com.mycompany.mavenproject1=>HelloWorld.java 测试包 com.test=>NewTestConfig.java和NewTestNGTest.java Helloworld.java package com.mycompany.mavenproject1; public class HelloWorld { String msg = "te

我是TestNg新手。我正在尝试测试一个示例hello world程序。 以下是我的项目结构

源程序包

  • com.mycompany.mavenproject1=>HelloWorld.java
测试包

  • com.test=>NewTestConfig.java和NewTestNGTest.java
Helloworld.java

package com.mycompany.mavenproject1;
public class HelloWorld {
  String msg = "test";

  public String getMsg() {
    return msg;
  }
  public void setMsg(String msg) {
    this.msg = msg;
  }
}
NewTestConfig.java

package com.test;
import com.mycompany.mavenproject1.HelloWorld;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class NewTestConfig {
     @Bean
    public HelloWorld helloWorldBean() {
        HelloWorld hw = new HelloWorld();
        return hw;
    }
}
NewTestNGTest.java

package com.test;

import com.mycompany.mavenproject1.HelloWorld;
import org.springframework.beans.factory.annotation.Autowired;
import org.testng.annotations.AfterClass;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
import org.springframework.test.context.ContextConfiguration;

@Test
@ContextConfiguration(classes={ NewTestConfig.class })
public class NewTestNGTest {
   @Autowired
    HelloWorld hw ;

    @Test
    public void exampleOfTestNgMaven() {
        String s = hw.getMsg();        
        System.out.println("This is TestNG-Maven Example" + s);
    }

    @BeforeClass
    public static void setUpClass() throws Exception {
    }

    @AfterClass
    public static void tearDownClass() throws Exception {
    }

    @BeforeMethod
    public void setUpMethod() throws Exception {
    }

    @AfterMethod
    public void tearDownMethod() throws Exception {
    }    
}
我的pom.xml如下所示:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.mycompany</groupId>
    <artifactId>mavenproject1</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>war</packaging>

    <name>mavenproject1</name>

    <properties>
        <endorsed.dir>${project.build.directory}/endorsed</endorsed.dir>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.testng</groupId>
            <artifactId>testng</artifactId>
            <version>6.8.1</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>4.2.1.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>4.2.1.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>javax</groupId>
            <artifactId>javaee-web-api</artifactId>
            <version>7.0</version>
            <scope>provided</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.1</version>
                <configuration>
                    <source>1.7</source>
                    <target>1.7</target>
                    <compilerArguments>
                        <endorseddirs>${endorsed.dir}</endorseddirs>
                    </compilerArguments>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <version>2.3</version>
                <configuration>
                    <failOnMissingWebXml>false</failOnMissingWebXml>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-dependency-plugin</artifactId>
                <version>2.6</version>
                <executions>
                    <execution>
                        <phase>validate</phase>
                        <goals>
                            <goal>copy</goal>
                        </goals>
                        <configuration>
                            <outputDirectory>${endorsed.dir}</outputDirectory>
                            <silent>true</silent>
                            <artifactItems>
                                <artifactItem>
                                    <groupId>javax</groupId>
                                    <artifactId>javaee-endorsed-api</artifactId>
                                    <version>7.0</version>
                                    <type>jar</type>
                                </artifactItem>
                            </artifactItems>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

</project>

4.0.0
com.mycompany
mavenproject1
1.0-快照
战争
mavenproject1
${project.build.directory}/project
UTF-8
org.testng
testng
6.8.1
测试
org.springframework
spring上下文
4.2.1.1发布
org.springframework
弹簧试验
4.2.1.1发布
爪哇
javaeewebapi
7
假如
org.apache.maven.plugins
maven编译器插件
3.1
1.7
1.7
${annowed.dir}
org.apache.maven.plugins
maven战争插件
2.3
假的
org.apache.maven.plugins
maven依赖插件
2.6
验证
复制
${annowed.dir}
真的
爪哇
javaee认可的api
7
罐子
当我尝试运行测试时,我得到了NullpointerException(在hw.getMsg();)

错误堆栈: 测试集:com.Test.NewTestNGTest
测试运行:1,失败:1,错误:0,跳过:0,运行时间:0.209秒您应该扩展一个Spring TestNg基类当前未对
@ContextConfiguration
注释进行任何操作,因为它只是一个普通的TestNg测试。您使用了什么maven命令运行?请阅读。非常感谢扩展AbstractTestNGSpringContextTests解决了我的问题。