@ org.testng.annotations.Test和@ org.junit.Test问题

时间:2017-11-23 05:51:26

标签: java eclipse maven junit mockito

在编写模拟测试用例时,我遇到了一个场景。当我使用@ org.junit.Test注释上面的测试方法而不是Maven构建时没有找到要执行的测试方法,当我使用@ org.testng.annotations.Test注释时,我无法运行测试用例eclipse Junit运行或使用覆盖率(EMMA)。 我不想在单一测试方法上使用两种注释。 我的代码如下,

@RunWith(MockitoJUnitRunner.class)
public class RDMActivateServiceImplTest {

    @InjectMocks
    private RDMActivateServiceImpl rdmActivateServiceImpl;
    @Mock
    private AddCheckInstanceStatus addInstanceStatus;
    @Mock
    private Timer timer;

    @BeforeMethod
    public void initMocks() {
        MockitoAnnotations.initMocks(this);
    }

    @org.testng.annotations.Test
    @org.junit.Test
    public void testAddAliveStatusInDb() {

        int intervalInSec = 0;
        Mockito.doNothing().when(timer).scheduleAtFixedRate(addInstanceStatus, 0, intervalInSec * 1000);
        assertNotNull(rdmActivateServiceImpl.addAliveStatusInDb(true));

    }

在放置@ org.junit.Test时,在maven build

上构建输出

Maven problem

并在日食上运行良好。

当我使用@ org.testng.annotations.Test时,eclipse输出是

Problem image

但是Maven完美地构建了测试用例。

我不想同时使用这两个注释,我正在寻找可以在两个地方运行测试用例的解决方案。

的pom.xml

<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.vnfm.vnfrdm</groupId>
  <artifactId>vnfrdm</artifactId>
   <packaging>war</packaging>
  <version>1.0.0</version>
    <properties>
        <java-version>1.8</java-version>
        <org.springframework-version>4.1.6.RELEASE</org.springframework-version>
        <org.aspectj-version>1.6.10</org.aspectj-version>
        <hibernate.version>4.3.10.Final</hibernate.version>
        <org.slf4j-version>1.7.21</org.slf4j-version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    </properties>

    <repositories>
        <repository>
            <id>clojars</id>
            <name>Clojars Repository</name>
            <url>http://clojars.org/repo</url>
        </repository>
        <repository>
            <id>project.local</id>
            <name>project</name>
            <url>file:${project.basedir}/../../vnfmDB/vnfmDb_lib/lib</url>
        </repository>
        <repository>
            <id>project.cdpl.tailf</id>
            <name>project</name>
            <url>file:${project.basedir}/cdpl_library</url>
        </repository>
        <repository>
            <id>project.local1</id>
            <name>project</name>
            <url>file:${project.basedir}/../common_library/lib</url>
        </repository>
        <repository>
            <id>project.oauth.common</id>
            <name>project</name>
            <url>file:${project.basedir}/../../oauth/common_library/client</url>
        </repository>
        <repository>
            <id>project.local2</id>
            <name>project</name>
            <url>file:${project.basedir}/../vnfm-common/common_library/lib</url>
        </repository>
    </repositories>


    <dependencies>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
            <version>4.3.10.Final</version>
            <exclusions>
                <exclusion>
                    <groupId>org.javassist</groupId>
                    <artifactId>javassist</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>com.vnfm-common</groupId>
            <artifactId>alarm_handler</artifactId>
            <version>0.0.1</version>
        </dependency>
        <dependency>
            <groupId>com.vnfm-common</groupId>
            <artifactId>audit_trail</artifactId>
            <version>0.0.1</version>
        </dependency>
        <dependency>
            <groupId>com.vnfm-common</groupId>
            <artifactId>counter_statistics</artifactId>
            <version>0.0.1</version>
        </dependency>
        <dependency>
            <groupId>com.vnfm-common</groupId>
            <artifactId>configuration_handler</artifactId>
            <version>0.0.1</version>
        </dependency>
        <dependency>
            <groupId>com.vnfm</groupId>
            <artifactId>vnfmDb</artifactId>
            <version>0.0.1</version>
        </dependency>
        <dependency>
            <groupId>com.swit</groupId>
            <artifactId>common</artifactId>
            <version>0.0.1</version>
        </dependency>
        <dependency>
            <groupId>com.vnfm</groupId>
            <artifactId>predestroy</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>
        <dependency>
            <groupId>com.vnfm.oauth.client</groupId>
            <artifactId>oauth-client-common</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>
        <dependency>
            <groupId>com.vnfm</groupId>
            <artifactId>mmcommon</artifactId>
            <version>0.0.1</version>
        </dependency>

        <dependency>
            <groupId>com.vnfm</groupId>
            <artifactId>AlarmClearing</artifactId>
            <version>0.1</version>
        </dependency>

        <!-- Jersey + Spring -->
        <dependency>
            <groupId>com.sun.jersey.contribs</groupId>
            <artifactId>jersey-spring</artifactId>
            <version>1.8</version>
            <exclusions>
                <exclusion>
                    <groupId>org.springframework</groupId>
                    <artifactId>spring</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>org.springframework</groupId>
                    <artifactId>spring-core</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>org.springframework</groupId>
                    <artifactId>spring-beans</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>org.springframework</groupId>
                    <artifactId>spring-context</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>org.springframework</groupId>
                    <artifactId>spring-web</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>org.springframework</groupId>
                    <artifactId>spring-aop</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpclient</artifactId>
            <scope>compile</scope>
            <version>4.3.3</version>
        </dependency>
        <!-- <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> 
            <version>5.1.36</version> </dependency> -->
        <dependency>
            <groupId>org.mariadb.jdbc</groupId>
            <artifactId>mariadb-java-client</artifactId>
            <version>1.4.4</version>
        </dependency>

        <dependency>
            <groupId>commons-validator</groupId>
            <artifactId>commons-validator</artifactId>
            <version>1.4.1</version>
        </dependency>

        <dependency>
            <groupId>org.postgresql</groupId>
            <artifactId>postgresql</artifactId>
            <version>9.3-1103-jdbc41</version>
        </dependency>

        <dependency>
            <groupId>org.hibernate.javax.persistence</groupId>
            <artifactId>hibernate-jpa-2.1-api</artifactId>
            <version>1.0.0.Final</version>

        </dependency>

        <dependency>
            <groupId>org.quartz-scheduler</groupId>
            <artifactId>quartz</artifactId>
            <version>2.2.3</version>
        </dependency>

        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpclient</artifactId>
            <version>4.5.1</version>
        </dependency>

        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpcore</artifactId>
            <version>4.4</version>
        </dependency>
        <dependency>
            <groupId>com.googlecode.json-simple</groupId>
            <artifactId>json-simple</artifactId>
            <version>1.1.1</version>
            <type>jar</type>
        </dependency>
        <dependency>
            <groupId>commons-fileupload</groupId>
            <artifactId>commons-fileupload</artifactId>
            <version>1.2.1</version>
        </dependency>
        <dependency>
            <groupId>commons-io</groupId>
            <artifactId>commons-io</artifactId>
            <version>1.3</version>
        </dependency>

        <dependency>
            <groupId>com.rabbitmq</groupId>
            <artifactId>amqp-client</artifactId>
            <version>3.5.4</version>
        </dependency>


        <!-- Spring -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>${org.springframework-version}</version>
            <exclusions>
                <!-- Exclude Commons Logging in favor of SLF4j -->
                <exclusion>
                    <groupId>commons-logging</groupId>
                    <artifactId>commons-logging</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>${org.springframework-version}</version>
        </dependency>

        <!-- AspectJ -->
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjrt</artifactId>
            <version>${org.aspectj-version}</version>
        </dependency>

        <!-- Logging -->
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>${org.slf4j-version}</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-log4j12</artifactId>
            <version>1.7.21</version>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.15</version>
            <exclusions>
                <exclusion>
                    <groupId>javax.mail</groupId>
                    <artifactId>mail</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>javax.jms</groupId>
                    <artifactId>jms</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>com.sun.jdmk</groupId>
                    <artifactId>jmxtools</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>com.sun.jmx</groupId>
                    <artifactId>jmxri</artifactId>
                </exclusion>
            </exclusions>
            <scope>runtime</scope>
        </dependency>

        <!-- @Inject -->
        <dependency>
            <groupId>javax.inject</groupId>
            <artifactId>javax.inject</artifactId>
            <version>1</version>
        </dependency>

        <!-- Servlet -->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>3.1.0</version>

        </dependency>
        <dependency>
            <groupId>javax.servlet.jsp</groupId>
            <artifactId>jsp-api</artifactId>
            <version>2.1</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
            <version>1.2</version>
        </dependency>
        <!-- <dependency> <groupId>taglibs</groupId> <artifactId>standard</artifactId> 
            <version>1.1.2</version> </dependency> <dependency> <groupId>javax.servlet</groupId> 
            <artifactId>jstl</artifactId> <version>1.1.2</version> </dependency> -->

        <!-- JSON -->
        <dependency>
            <groupId>org.codehaus.jackson</groupId>
            <artifactId>jackson-mapper-asl</artifactId>
            <version>1.9.10</version>
        </dependency>
        <dependency>
            <groupId>org.json</groupId>
            <artifactId>json</artifactId>
            <version>20090211</version>
        </dependency>

        <!-- JAXB -->
        <dependency>
            <groupId>xerces</groupId>
            <artifactId>xercesImpl</artifactId>
            <version>2.11.0</version>
        </dependency>

        <!-- Test -->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.7</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-tx</artifactId>
            <version>${org.springframework-version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>${org.springframework-version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-orm</artifactId>
            <version>${org.springframework-version}</version>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-entitymanager</artifactId>
            <version>4.3.10.Final</version>

            <!-- will come with Hibernate core -->
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-beans</artifactId>
            <version>${org.springframework-version}</version>
        </dependency>

        <dependency>
            <groupId>javassist</groupId>
            <artifactId>javassist</artifactId>
            <version>3.12.1.GA</version>
        </dependency>
        <dependency>
            <groupId>commons-logging</groupId>
            <artifactId>commons-logging</artifactId>
            <version>1.1.2</version>
        </dependency>

        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-api</artifactId>
            <version>2.4.1</version>
        </dependency>
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-core</artifactId>
            <version>2.4.1</version>
        </dependency>
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-1.2-api</artifactId>
            <version>2.4.1</version>
        </dependency>
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-jcl</artifactId>
            <version>2.4.1</version>
        </dependency>

        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-web</artifactId>
            <version>2.4.1</version>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-iostreams</artifactId>
            <version>2.4.1</version>
        </dependency>
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-jul</artifactId>
            <version>2.4.1</version>
        </dependency>

        <dependency>
            <groupId>org.jboss.logging</groupId>
            <artifactId>jboss-logging</artifactId>
            <version>3.3.0.Final</version>
        </dependency>
        <dependency>
            <groupId>org.testng</groupId>
            <artifactId>testng</artifactId>
            <version>6.8.5</version>
        </dependency>
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-math3</artifactId>
            <version>3.1.1</version>
        </dependency>
       <!--  <dependency>
            <groupId>com.tailf</groupId>
            <artifactId>conf-api-src</artifactId>
            <version>6.2</version>
        </dependency>
        <dependency>
            <groupId>com.tailf</groupId>
            <artifactId>conf-api</artifactId>
            <version>6.2</version>
        </dependency> -->
        <dependency>
            <groupId>org.onosproject</groupId>
            <artifactId>jnc</artifactId>
            <version>1.0</version>
        </dependency>
        <dependency>
            <groupId>org.mockito</groupId>
            <artifactId>mockito-core</artifactId>
            <version>2.0.73-beta</version>
        </dependency>
    </dependencies>

    <build>
        <finalName>vnfrdm</finalName>
        <plugins>
            <plugin>
                <artifactId>maven-eclipse-plugin</artifactId>
                <version>2.9</version>
                <configuration>
                    <additionalProjectnatures>
                        <projectnature>org.springframework.ide.eclipse.core.springnature</projectnature>
                    </additionalProjectnatures>
                    <additionalBuildcommands>
                        <buildcommand>org.springframework.ide.eclipse.core.springbuilder</buildcommand>
                    </additionalBuildcommands>
                    <downloadSources>true</downloadSources>
                    <downloadJavadocs>true</downloadJavadocs>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.3</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                    <compilerArgument>-Xlint:all</compilerArgument>
                    <showWarnings>true</showWarnings>
                    <showDeprecation>true</showDeprecation>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>exec-maven-plugin</artifactId>
                <version>1.2.1</version>
                <configuration>
                    <mainClass>org.test.int1.Main</mainClass>
                </configuration>
            </plugin>
        </plugins>
    </build> 
</project>

1 个答案:

答案 0 :(得分:2)

你真的不应该同时使用它们。 决定一个并坚持下去。他们都很好并且分享了大部分功能。

一旦选择了一个,使用Maven Surefire插件将其明确定义为maven测试阶段目标。

然后直接获取Eclipse插件。如果我没记错的话,Eclipse会预装一个JUnit插件,但你需要通过Eclipse市场明确下载TestNG插件。它们都非常相似。

Spring支持两者,但实现略有不同。查看文档以获取示例。