我正在尝试获取终端命令来执行SpringBootTest。与 mvn spring-boot:run 类似。该命令触发SpringBootApplication。
但是我想使用带有运行时maven参数的maven命令从终端触发SpringBootTest。你们中有人知道触发SpringBootTest的任何类似命令吗?
我的代码如下:
@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();
}
我正在以编程方式使用spring触发testng。我需要在testng测试用例中进行依赖注入的spring框架。请帮忙。我尝试了所有可能性
答案 0 :(得分:0)
您可能要运行
quay.io/quarkus/centos-quarkus-maven:20.0.0-java11
在包含pom.xml文件的目录中的只能执行一个测试类。
但是正如@ M.Deinum所说,您的设置似乎有缺陷,试图在Junit测试中执行TestNG测试。
答案 1 :(得分:0)
感谢您提出的所有建议。经过大量的反复试验,我找到了解决方案。这只是一个解决方法。我没有将主类作为Springboottest运行,而是通过maven命令将其触发为Main类,作用域为test。请找到我的代码和触发代码的解决方案,
触发测试套件的主类
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");
}
}
二号豆
import org.springframework.stereotype.Component;
@Component
public class BeanTwo {
public BeanTwo createBean(){
return new BeanTwo();
}
public void printBeanTwo(){
System.out.println("Bean Two!!");
}
}
TestNG类:
@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配置
...
<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>
...
输出:
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: 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
触发主类的解决方案:
mvn exec:java -Dexec.mainClass=“org.test.Sample” -Dexec.classpathScope=test