Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MavenProject coming null inside ITestContext #3141

Closed
1 of 7 tasks
mehulparmariitr opened this issue Jun 24, 2024 · 9 comments
Closed
1 of 7 tasks

MavenProject coming null inside ITestContext #3141

mehulparmariitr opened this issue Jun 24, 2024 · 9 comments

Comments

@mehulparmariitr
Copy link

mehulparmariitr commented Jun 24, 2024

TestNG Version

Note: only the latest version is supported

Expected behavior

iTestContext.getAttribute("maven.project") should not be null

Actual behavior

@Override
    public void onStart(final ITestContext iTestContext) {
        mavenProject = (MavenProject) iTestContext.getAttribute("maven.project");
        System.out.println("project found!!"+ mavenProject.getName());

mavenProject is coming as null

Is the issue reproducible on runner?

  • Shell
  • Maven
  • Gradle
  • Ant
  • Eclipse
  • IntelliJ
  • NetBeans

Test case sample

Please, share the test case (as small as possible) which shows the issue
I am creating a custom TestNG listener and this is happening inside it.

public final class TestDataCollectListener implements IExecutionListener, ITestListener, ISuiteListener {

@Override
    public void onStart(final ITestContext iTestContext) {
        mavenProject = (MavenProject) iTestContext.getAttribute("maven.project");
        System.out.println("project found!!"+ mavenProject.getName());
}
}

What am I missing here. I even added maven-surefire-plugin but no change.

<plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <configuration>
                    <suiteXmlFiles>
                        <suiteXmlFile>${suiteXmlFile}</suiteXmlFile>
                    </suiteXmlFiles>
                    <properties>
                        <property>
                            <name>usedefaultlisteners</name>
                            <value>false</value>
                        </property>
                        <property>
                            <name>listener</name>
                            <value>com.abc.test.pqr.TestDataCollectListener</value>
                        </property>
                    </properties>
                </configuration>
            </plugin>

Contribution guidelines

Incase you plan to raise a pull request to fix this issue, please make sure you refer our Contributing section for detailed set of steps.

@juherr
Copy link
Member

juherr commented Jun 24, 2024

Hi,

MavenProject cannot be present if nobody adds it.
TestNG doesn't manage the maven runner.
Maybe you should try to open an issue at the surefire-maven-plugin project.

@krmahadevan
Copy link
Member

@mehulparmariitr - Adding to what @juherr already called out. TestNG does not have any context with respect to the build tool that is executing it (which would be maven in this case). If you are just after a bunch of maven properties, then you can perhaps use the maven resources plugin as explained in this stackoverflow post and then read the properties file via your TestNG listener to get it to work. The other option would be to file a bug in the Maven surefire plugin jira page and get them to have this enabled at the maven surefire plugin side.

Closing this issue since there's not much that is required to be done from TestNG side here.

@mehulparmariitr
Copy link
Author

I don't want to read my properties file. Just want to read pom.xml of all modules, so therefore need to create a MavenProject object and load the whole project into it.

@krmahadevan
Copy link
Member

In that case, you should just add the Maven core artifact as a dependency and then directly work with it. https://stackoverflow.com/a/4838664/679824

@mehulparmariitr
Copy link
Author

mehulparmariitr commented Jun 25, 2024

Caveat there is, pom.xml location of my current module I will have to fetch from System.getProperty("user.dir")and then load the MavenProject.
This might not always give the correct path of pom.xml as users can run the java command from other location and System.getProperty("user.dir") will give incorrect path.

@krmahadevan
Copy link
Member

The properties file is also going to be generated by Maven and not going to be created by you. So when you build/test your project, the maven resources plugin kicks in and starts replacing the variables with actual values obtained from the maven context. In either case, please play around with it to get a bit more better understanding as to what it does.

@krmahadevan
Copy link
Member

krmahadevan commented Jun 25, 2024

Caveat there is, pom.xml location of my current module I will have to fetch from System.getProperty("user.dir")and then load the MavenProject. This might not always give the correct path of pom.xml as users can run the java command from other location and System.getProperty("user.dir") will give incorrect path.

You wouldn't need to be dealing with any of that, if you just add the maven resources plugin to your parent pom and then have it do the heavy lifting for you in terms of dumping whatever maven data you need into a properties file.

@mehulparmariitr
Copy link
Author

Okay I see idea is to create a template properties file and Maven will load it when maven resources plugin is kicked in root pom.xml.
But this will involve extra efforts in user's maven project. I am the owner of a custom TestNG dependency which resides in integrationTests module. And other users are just using this dependency. Any way we can read all pom.xml without asking users to modify much in their maven project ?

@krmahadevan
Copy link
Member

No there are no other ways. You are basically asking for awareness of a specific build tool, with a test runner.

Please remember that TestNG is a test runner that can be run via a build tool (Ant/Maven/Gradle/Bazel) or it can even be run via a plain vanilla java project.

So, there's no way in which TestNG can have that sort of intelligence baked into it. You have one of the following options.

  1. Build your own custom implementation of surefire plugin wherein this ability is available.
  2. Enhance your test library to be able to scan through the file system to retrieve pom files and then parse them. You may need to ask the user to specify the root directory via some custom JVM argument instead of relying on user.dir.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants