2

I am trying to call test classes from src main using maven but unable compile using maven my project structure is like below

enter image description here

My main class looks like

package com.automation.selenium.demo;

import com.automation.selenium.test.demo.TestClass;

public class EntryClass {

    public static void main(String[] args) {
        System.out.println("!!!!!!!!!!!!!!! Calling From Main Class !!!!!!!!!!!!");
        TestClass testNGtest = new TestClass();
        testNGtest.testMethod();
    }
}

My test class looks like

package com.automation.selenium.test.demo;

public class TestClass {

    public void testMethod() {
        System.out.println("!!!!!!!!!!!!!!! Calling From Test Class !!!!!!!!!!!!");
    }
}

My pom.xml is

<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.ibm.automation.selenium.demo</groupId>
    <artifactId>Demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <dependencies>
        <!-- https://mvnrepository.com/artifact/org.testng/testng -->
        <dependency>
            <groupId>org.testng</groupId>
            <artifactId>testng</artifactId>
            <version>6.14.3</version>
            <scope>test</scope>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>exec-maven-plugin</artifactId>
                <version>1.6.0</version>
                <configuration>
                    <executable>java</executable>
                    <arguments>
                        <argument>-classpath</argument>
                        <classpath/>
                        <argument>${project.build.directory}\test-classes\com\automation\selenium\test\demo</argument>
                    </arguments>
                    <!-- <testSourceRoot>${project.basedir}/src/test{</testSourceRoot> -->
                    <mainClass>com.automation.selenium.demo.EntryClass</mainClass>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

I have use the following command to run

clean compiler:testCompile exec:java

but it is showing the following error

[ERROR] Failed to execute goal org.codehaus.mojo:exec-maven-plugin:1.6.0:java (default-cli) on project Demo: Unable to parse configuration of mojo org.codehaus.mojo:exec-maven-plugin:1.6.0:java for parameter arguments: Cannot store value into array: ArrayStoreException -> [Help 1]
[ERROR] 
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR] 
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/PluginConfigurationException

I am new in Maven anyone please help me to findout the issue.

saba
  • 539
  • 1
  • 14
  • 30
  • Why do you want to use test classes in production code? – Slaw Apr 29 '19 at 06:33
  • Actually, it is required to design a test framework – saba Apr 29 '19 at 06:35
  • If you're designing a test framework based on TestNG, change dependency scope to *provided*. Test scoped dependencies not visible for the source code. Instead of `test` change to `provided` – Sachith Dickwella Apr 29 '19 at 06:39
  • @SachithDickwella Thanks for your comment but it is not working As per my above code I have not mentioned any TestNG related things. – saba Apr 29 '19 at 07:26
  • @saba, If you define classes in `test` directory, there's no way to access them outside of testing scope. You can't use them in your application source. If you're trying to execute a test-case here, you don't need to invoke test functions explicitly. You just have to prepare a test suite and decorate required test function with `@Test` annotation (I can see you're using `TestNG` and `JUnit` also the same with small changes). If you're trying to develop your own testing framework base on TestNG, best thing is use this dependency as `provided` one. – Sachith Dickwella Apr 29 '19 at 07:43
  • @SachithDickwella Thanks for your comment yes actually what I want to do that I want to call TestNG programmatically so that's why I need to use main and I want to keep main in src folder because main is basically application related code. – saba Apr 29 '19 at 08:52

1 Answers1

3

A class in src/main/java should never call a class in src/test/java. Classes in src/test/java are meant to test the classes in src/main/java. They are not meant to be used as dependencies or parts of self-constructed test frameworks.

If you want to build a jar for testing, put the classes into src/main/java.

J Fabian Meier
  • 33,516
  • 10
  • 64
  • 142
  • Thanks for your comment yes actually what I want to do that I want to call TestNG programmatically so that's why I need to use main and I want to keep main in src folder because main is basically application related code. – saba Apr 29 '19 at 08:53
  • Yes presently I am following this but actually I try to make it more structured so that's why I put this question on this forum but thanks for your time and thoughts. – saba Apr 29 '19 at 09:07
  • 1
    @saba it is good to put more structure in things, but `src/test/java` is just not meant to be used in the way you tried to use it. – J Fabian Meier Apr 29 '19 at 09:16
  • So i cannot create a jar file just to execute all the test cases? I want to write all the test case scenario and give it to my testing team. To make his life simpler i want to create a executable jar and if he wants to run any test cases he can just double click on the corresponding file. – Ananth Kamath Sep 30 '21 at 12:39
  • @AnanthKamath What would be the purpose of such a JAR? You usually run the tests during the build and if they pass, the build succeeds. Why should the testing team run the same tests again against the same code? If the tests are green the first time, they will always be green. – J Fabian Meier Sep 30 '21 at 17:01
  • @JFabianMeier, I work on Mac and Windows native app development. So, to create my application I either use XCODE or Visual Studio. So, to write test cases I am using 3rd party framework like Appium. The reason is to have and run same test case scenarios in both the platform(Mac & Win). This is possible only through Appium, coz it has support for both the platform. Now why I need a JAR file is, I want to remove the dependency on the IDEs and execute the test cases without them. – Ananth Kamath Oct 06 '21 at 15:30
  • Anyway I figured out a way to create a executable JAR file. It is working fine as expected. – Ananth Kamath Oct 06 '21 at 15:31