ContextConfiguration似乎没有为TestNg测试用例加载我的bean

时间:2015-10-12 12:28:03

标签: 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 = "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>

当我尝试运行测试时,我得到NullpointerException(在hw.getMsg();)

错误堆栈:

测试集:com.test.NewTestNGTest

测试运行:1,失败:1,错误:0,跳过:0,经过的时间:0.209秒&lt;&lt;&lt;失败! exampleOfTestNgMaven(com.test.NewTestNGTest)经过的时间:0.003秒&lt;&lt;&lt;失败! 显示java.lang.NullPointerException     在com.test.NewTestNGTest.exampleOfTestNgMaven(NewTestNGTest.java:25)

我试图在调试模式下运行测试,我总是将HelloWorld bean作为null。因此,最终测试因NullPointerException而失败。我尝试了许多其他变体,比如使用applicationContext.xml尝试使用config类,但我的bean永远不会被注入。

在过去的两天里,我很难做出简单的测试通过但却无法弄清楚出了什么问题。为什么我的上下文没有加载进行测试?

非常感谢任何帮助。

由于 MANISHA

0 个答案:

没有答案