简体   繁体   中英

can't able to call code inside src/test/java from src/main/java?

I am using testNG for writing test cases, all testcase code is inside src/test/java folder, now i want to run testcases inside this folder from a class inside src/main/java after deployment of the application,will it be possible?

file under src/main/java-> TestNGRun

public class TestNGRun {
    public static void runTestNg() {
        TestListenerAdapter tla = new TestListenerAdapter();
        TestNG testNG = new TestNG();
        testNG.addListener(tla);
        List<String> suites = new ArrayList<String>();
        URL filePathUrl = TestNGRun.class.getClassLoader().getResource("/testng.xml");
        suites.add(filePathUrl.getPath());
        testNG.setTestSuites(suites);
        testNG.run();
    }
}

file under src/main/resources--> testng.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd">
<suite name="Suite">
    <listeners>
        <listener
            class-name="com.cypress.prodService.testNG.CustomisedReports"></listener>
    </listeners>
    <test name="ProductServiceImplTest">
        <classes>
            <class
                name="com.cypress.prodService.testNG.ProductServiceImplTest"></class>
        </classes>
    </test>
</suite>

file under src/test/java --> ProductServiceImplTest.java

@Test
public class ProductServiceImplTest {
    @Test
        public void addProduct() {`enter code here`
            String value="GoodMorning";
            assertEquals("Hello", value);
        }
}

Have you tried putting them in the same package? That's usually how you set up JUnit Tests. This is a JUnit5 example, but using the JUnit Platform Launcher, discovering tests by selecting the package works:

https://github.com/doenn/sentences/tree/master/src https://github.com/doenn/sentences/blob/master/src/test/java/sentences/SentencesTest.java https://github.com/doenn/sentences/blob/master/src/main/java/sentences/SentenceTests.java

On the testNG side, yes should be able to put tests in src/test also. See this answer on example folder layouts with testNG.

It might depend on how your project is set up, but I know for all of my projects, anything in the test folder IntelliJ will not let you call from the main folder, only the other way around. idk if this is a java thing or specifically from the IDE, but usually you wouldn't want to rely on anything from your test packages in your actual build.

To add, I'm pretty sure the test package of your projects are designed specifically to test your code outside of it being built, and arent included in the actual build of the program when it's running. So any calls to the classes within would not actually have anything to call to during the program running. If you are trying to create some form of test that is ran during the programs runtime, I'd create it in src/main/ then create a package for your verification tests there, where they will be included in the build. Just keep in mind that these tests will not run in any CICD style building check by default, so if all your tests are in there, you might get a bugged out program with failing tests past the checks without failing.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM