How to execute @SpringBootTest from terminal using mvn command?

Viewed 5838

I am trying to get the terminal command to execute SpringBootTest. Something similar to mvn spring-boot:run. This command triggers SpringBootApplication.

But I want to trigger SpringBootTest from the terminal using maven command with runtime maven arguments. Do any of you guys know any similar command for triggering SpringBootTest?

My code looks like below:

@SpringBootTest
class SeosSdkAutomationApplicationTests {

    @Test
    void contextLoads() {
        System.out.println("Hello!!");
        TestNG tng = new TestNG();
        XmlSuite suite = new XmlSuite();
        suite.setName("Appium Test suite");
        XmlTest test = new XmlTest(suite);
        test.setName("Sample Test");
        List<XmlClass> classes = new ArrayList<>();
        classes.add(new XmlClass("com.org.TestNGClass_sample"));
        test.setXmlClasses(classes);
        List<XmlSuite> suites = new ArrayList<>();
        suites.add(suite);
        tng.setXmlSuites(suites);
        tng.run();

    }

I am programmatically triggering testng using spring. I need spring framework for dependency injection in testng test cases. Please help out. I tried all the possibilities

2 Answers

You might want to run

mvn -Dtest=SeosSdkAutomationApplicationTests test

in the directory that contains your pom.xml file to execute just this one test class.

But as @M.Deinum said, your setup looks flawed, trying to execute a TestNG test inside a Junit test.

Thanks for all your suggestions friends. After a lot of trial and error, I got a solution. It's just a workaround. Instead of running the main class as a Springboottest, I triggered it as the Main class with scope as test, from maven command. Please find my code and the solution to trigger the code,

Main Class to trigger test suite

import java.util.List;

class Sample {

public static void main(String[] args) {
        System.out.println("Hello!!");
        TestNG tng = new TestNG();
        XmlSuite suite = new XmlSuite();
        suite.setName("Appium Test suite");
        XmlTest test = new XmlTest(suite);
        test.setName("Sample Test");
        List<XmlClass> classes = new ArrayList<>();
        classes.add(new XmlClass("org.test.TestNGClass_sample"));
        test.setXmlClasses(classes);
        List<XmlSuite> suites = new ArrayList<>();
        suites.add(suite);
        tng.setXmlSuites(suites);
        tng.run();

}

Bean One:

import org.springframework.stereotype.Component;

@Component
public class BeanOne {

    public BeanOne createBean(){
        return new BeanOne();
    }

    public void printBeanOne(){
        System.out.println("Bean One");
    }

}

Bean Two

import org.springframework.stereotype.Component;

@Component
public class BeanTwo {

    public BeanTwo createBean(){
        return new BeanTwo();
    }

    public void printBeanTwo(){
        System.out.println("Bean Two!!");
    }
}

TestNG Class:

@SpringBootTest
public class TestNGClass_sample extends AbstractTestNGSpringContextTests {

    @Autowired
    public BeanOne beanOne;

    @Autowired
    public BeanTwo beanTwo;

    @Test
    public void test(){
        beanOne.printBeanOne();
        beanTwo.printBeanTwo();
    }

    @Test
    public void test2(){
        System.out.println("Test2");
    }

    @Test
    public void test3(){
        System.out.println("Test3");
    }
}

pom.xml configuration

...
    <build>
            <plugins>
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                </plugin>
                <plugin>
                    <groupId>org.codehaus.mojo</groupId>
                    <artifactId>exec-maven-plugin</artifactId>
                    <version>1.2.1</version>
                    <configuration>
                        <mainClass>org.test.Sample</mainClass>
                    </configuration>
                </plugin>
            </plugins>
        </build>
...

Output:

.   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v2.2.6.RELEASE)

2020-03-31 22:51:00.391  INFO 24148 --- [ionTests.main()] s.s.a.s.TestNGClass_sample               : Started TestNGClass_sample in 0.653 seconds (JVM running for 3.29)
Bean One
Bean Two!!
Test2
Test3

===============================================
Appium Test suite
Total tests run: 3, Passes: 3, Failures: 0, Skips: 0

Solution to trigger the main class:

mvn exec:java -Dexec.mainClass=“org.test.Sample” -Dexec.classpathScope=test
Related