UltraKill
UltraKill

Reputation: 1

Maven won't recognise testNG annotation for files in '/src/main/java' when maven test is executed through cmd

I am facing particular issue that when I run the project as maven test in eclipse it runs fine. But when I try to execute the same in command line, build fails with errors like followings -

//src/main/java/com/qa/util/TestUtil.java:[10,18] package org.testng does not exist [ERROR] //src/main/java/com/qa/util/TestUtil.java:[11,30] package org.testng.annotations does not exist [ERROR] //src/main/java/com/qa/util/TestUtil.java:[64,35] cannot find symbol [ERROR] symbol: class ITestContext [ERROR] location: class com.eBusiness.qa.util.TestUtil

Out of curiosity I also tried moving entire /java folder under test folder but the issue remain. What is wrong in my configuration?

My TestUtil.java file:

package com.qa.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

import org.apache.commons.io.FileUtils;
import org.testng.annotations.DataProvider;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;


import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;

import com.eBusiness.qa.base.TestBase;

public class TestUtil extends TestBase{
    
    public static long PAGE_LOAD_TIMEOUT = 60;
    public static long IMPLICIT_WAIT = 20;  
    
    static Workbook book;
    static Sheet sheet;

    @DataProvider(name="test")
    public static Object[][] getTestData2() throws IOException { 
        FileInputStream file = new FileInputStream("C:\\Users\\Teju\\Downloads\\payrollDps.xlsx");
        sheet = book.getSheet("allowance");
        Object[][] data = new Object[sheet.getLastRowNum()][sheet.getRow(0).getLastCellNum()];
        int noOfColumns = sheet.getRow(0).getLastCellNum();
        //System.out.println(noOfColumns);
        String[] Headers = new String[noOfColumns];
        for (int j=0;j<noOfColumns;j++){
            Headers[j] = sheet.getRow(0).getCell(j).getStringCellValue();
        }
            for (int a=0;a<noOfColumns;a++){
                if(Headers[a].equals("Allowance Name")){
                    for (int i = 1; i < sheet.getLastRowNum(); i++) {
                        data[i][a] = sheet.getRow(i).getCell(a).toString();
                        }
                 }
            }
            try {book.close();} catch (IOException e) { e.printStackTrace();}
          return data;
    }   
        
    @DataProvider(name="Data")
    public Object[][] getDataFromDataprovider(){
    return new Object[][] 
        {
            { "ATitle" , "Adesc"},
        };
    }
    
    public static void refreshPage(){
        driver.navigate().refresh();
    }
    
    public static void takeScreenshotAtEndOfTest() throws IOException {
        File scrFile = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
        String currentDir = System.getProperty("user.dir");     
        FileUtils.copyFile(scrFile, new File(currentDir + "/screenshots/" + System.currentTimeMillis() + ".png"));
        
        }   
}

Also my pom if needed:

4.0.0

<groupId>HRMS</groupId>
<artifactId>eBusinessAutomation</artifactId>
<version>1.0-SNAPSHOT</version>

<name>project</name>
<!-- FIXME change it to the project's website -->
<url>https://www.github.com</url>

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>
</properties>

<repositories>
    <repository>
        <id>jcenter</id>
        <name>bintray</name>
        <url>https://jcenter.bintray.com</url>
    </repository>
</repositories>

<dependencies>
    
    <dependency>
        <groupId>com.google.guava</groupId>
        <artifactId>guava</artifactId>
        <version>21.0</version>
    </dependency>
    
    <dependency>
        <groupId>org.seleniumhq.selenium</groupId>
        <artifactId>selenium-java</artifactId>
        <version>4.0.0-alpha-7</version>
    </dependency>
    
    <dependency>
        <groupId>org.testng</groupId>
        <artifactId>testng</artifactId>
        <version>6.10</version>
        <scope>compile</scope>
    </dependency>

    <dependency>
        <groupId>org.junit</groupId>
        <artifactId>com.springsource.org.junit</artifactId>
        <version>4.11.0</version>
    </dependency>

    <dependency>
       <groupId>org.slf4j</groupId>
       <artifactId>slf4j-api</artifactId>
       <version>1.7.5</version>
   </dependency>
   <dependency>
       <groupId>org.slf4j</groupId>
       <artifactId>slf4j-log4j12</artifactId>
       <version>1.7.5</version>
   </dependency>
    <dependency>
        <groupId>org.apache.poi</groupId>
        <artifactId>poi</artifactId>
        <version>4.1.2</version>
    </dependency>

    <dependency>
        <groupId>org.apache.poi</groupId>
        <artifactId>openxml4j</artifactId>
        <version>1.0-beta</version>
    </dependency>

    <dependency>
        <groupId>org.apache.commons</groupId>
        <artifactId>commons-io</artifactId>
        <version>1.3.2</version>
    </dependency>

    <dependency>
        <groupId>com.paulhammant</groupId>
        <artifactId>ngwebdriver</artifactId>
        <version>1.1.3</version>
    </dependency>

    <!-- https://mvnrepository.com/artifact/com.relevantcodes/extentreports -->
    <dependency>
        <groupId>com.relevantcodes</groupId>
        <artifactId>extentreports</artifactId>
        <version>2.41.2</version>
    </dependency>

    <dependency>
        <groupId>org.apache.poi</groupId>
        <artifactId>ooxml-schemas</artifactId>
        <version>1.4</version>
    </dependency>
    <dependency>
        <groupId>org.apache.poi</groupId>
        <artifactId>poi-ooxml</artifactId>
        <version>4.1.2</version>
    </dependency>
    <dependency>
        <groupId>org.apache.poi</groupId>
        <artifactId>poi-ooxml-schemas</artifactId>
        <version>4.1.2</version>
    </dependency>
    <dependency>
        <groupId>org.apache.poi</groupId>
        <artifactId>poi-scratchpad</artifactId>
        <version>4.1.2</version>
    </dependency>
</dependencies>

<build>
    <pluginManagement><!-- lock down plugins versions to avoid using Maven 
            defaults (may be moved to parent pom) -->
        <plugins>
            <!-- clean lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#clean_Lifecycle -->
            <plugin>
                <artifactId>maven-clean-plugin</artifactId>
                <version>3.1.0</version>
            </plugin>
            <!-- default lifecycle, jar packaging: see https://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging -->
            <plugin>
                <artifactId>maven-resources-plugin</artifactId>
                <version>3.0.2</version>
            </plugin>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.0</version>
            </plugin>
            <plugin>
                <artifactId>maven-jar-plugin</artifactId>
                <version>3.0.2</version>
            </plugin>
            <plugin>
                <artifactId>maven-install-plugin</artifactId>
                <version>2.5.2</version>
            </plugin>
            <plugin>
                <artifactId>maven-deploy-plugin</artifactId>
                <version>2.8.2</version>
            </plugin>
            <!-- site lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#site_Lifecycle -->
            <plugin>
                <artifactId>maven-site-plugin</artifactId>
                <version>3.7.1</version>
            </plugin>
            <plugin>
                <artifactId>maven-project-info-reports-plugin</artifactId>
                <version>3.0.0</version>
            </plugin>
             <!-- This is added to create java project -->
            <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-shade-plugin</artifactId>
            <version>3.2.0</version>
            <executions>
                <!-- Attach the shade into the package phase -->
                <execution>
                    <phase>package</phase>
                    <goals>
                        <goal>shade</goal>
                    </goals>
                    <configuration>
                        <transformers>
                            <transformer
                                    implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                                <mainClass>com.mkyong.hashing.App</mainClass>
                            </transformer>
                        </transformers>
                    </configuration>
                </execution>
            </executions>
        </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>3.0.0-M5</version>
                <inherited>true</inherited>
                    <configuration>
                        <useSystemClassLoader>true</useSystemClassLoader>
                        <suiteXmlFiles>code_test_run</suiteXmlFiles>
                    </configuration>
            </plugin>
        </plugins>
    </pluginManagement>
</build>

Upvotes: 0

Views: 2138

Answers (3)

swngstack
swngstack

Reputation: 69

I was facing same issue and followed what Rahul Das said but made a slight change; I updated the scope of the dependency to <scope>compile</scope>.

So finally the dependency will look like:

 <dependency>
    <groupId>org.testng</groupId>
    <artifactId>testng</artifactId>
    <version>7.1.0</version>
    <scope>compile</scope>
 </dependency>

That update in scope did the trick for me

Upvotes: 2

UltraKill
UltraKill

Reputation: 1

In case anybody faced same problem, take a look at the classpath of your project. It should point to the actual repository. In my case I was initially pointing it towards the eclipse project when I ran it in cmd thus it kept throwing error.

Upvotes: 0

Rahul Das
Rahul Das

Reputation: 74

Change the scope of the dependency

 <dependency>
        <groupId>org.testng</groupId>
        <artifactId>testng</artifactId>
        <version>6.10</version>
        <scope>runtime</scope>
  </dependency>

or you can try remove the scope

<dependency>
      <groupId>org.testng</groupId>
      <artifactId>testng</artifactId>
      <version>6.10</version>
</dependency>

are you placing the test classes inside the default test package, then its need to be changed to under src (if its a automation project)

Upvotes: 0

Related Questions