如何解决JUNIT @ParameterizedTest ,参数解析异常
Eclipse 版本 - 4.19.0 Maven 版本 - 3.8.1
在 JUnit 中,我尝试使用 @CsvSource 或 @MethodSource 实现 @ParameterizedTest。由于 org.junit.jupiter.api.extension.ParameterResolutionException,两者都失败了:
有人可以帮忙看看我做错了什么。
以下是我实现堆栈功能的类。
var notification: UNNotification?
func didReceive(_ notification: UNNotification) {
self.notification = notification
...
}
@IBAction func btnActionHappy(_ sender: Any) {
if #available(iOSApplicationExtension 12.0,*) {
extensionContext?.dismissNotificationContentExtension()
}
if let identifier = notification?.request.identifier {
let center = UNUserNotificationCenter.current()
center.removeDeliverednotifications(withIdentifiers: [identifier])
}
}
以下是我的测试课
package com.skill.apps;
import java.util.Stack;
import org.testng.Assert;
public class StackOperations
{
static Stack<Integer> stack;
public StackOperations()
{
stack = new Stack<Integer>();
}
public boolean push(Integer element)
{
int iBeforePush = stack.size();
stack.push(element);
if(iBeforePush < stack.size())
return true;
else
return false;
}
public boolean pop(Integer element)
{
//int iLastElement = stack.lastElement();
System.out.println(stack.lastElement());
stack.pop();
if(element != stack.lastElement())
return true;
else
return false;
}
public int peek(Integer element)
{
Integer iElement = stack.peek();
if(iElement == element)
return iElement;
else
return 0;
}
public Integer[] display()
{
Integer[] T = null;
Integer[] iList = stack.toArray(T);
return iList;
}
}
以下是我的pom文件
package com.skill.test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.testng.Assert;
import com.skill.apps.StackOperations;
public class StackOperationsTest {
private Integer input;
private Boolean expected;
static StackOperations obj = new StackOperations();
public StackOperationsTest(int input,boolean expected) {
this.input = input;
this.expected = expected;
}
/*//@Parameters
private static Stream<Arguments> testDataForPush()
{
return Stream.of(
Arguments.of(100,Boolean.TRUE),Arguments.of(200,Arguments.of(300,Boolean.TRUE)
);
}*/
@ParameterizedTest(name = "#{index} - Test with String : {0}")
@CsvSource({
"100,Boolean.TRUE","200,Boolean.TRUE"
})
//@MethodSource("testDataForPush")
public void testPush()
{
Boolean bResult = obj.push(input);
Assert.assertEquals(bResult,expected);
}
}
以下是堆栈跟踪:
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>SLBFirstProject</groupId>
<artifactId>SLBFirstProject</artifactId>
<version>0.0.1-SNAPSHOT</version>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<aspectj.version>1.8.10</aspectj.version>
</properties>
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
<!-- https://mvnrepository.com/artifact/junit/junit -->
<dependencies>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-all</artifactId>
<version>1.10.19</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-junit-jupiter</artifactId>
<version>2.23.4</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.5.2</version>
<scope>test</scope>
</dependency>
<!-- Parameterized Tests -->
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-params</artifactId>
<version>5.5.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>3.141.5</version>
</dependency>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>7.3.0</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>4.1.2</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>4.1.2</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml-schemas</artifactId>
<version>4.1.2</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-scratchpad</artifactId>
<version>4.1.2</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>ooxml-schemas</artifactId>
<version>1.4</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>openxml4j</artifactId>
<version>1.0-beta</version>
</dependency>
<dependency>
<groupId>com.relevantcodes</groupId>
<artifactId>extentreports</artifactId>
<version>2.41.2</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.8.0</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
<dependency>
<groupId>com.beust</groupId>
<artifactId>jcommander</artifactId>
<version>1.72</version>
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-support</artifactId>
<version>3.141.59</version>
</dependency>
<dependency>
<groupId>com.googlecode.json-simple</groupId>
<artifactId>json-simple</artifactId>
<version>1.1.1</version>
</dependency>
<dependency>
<groupId>io.qaMeta.allure</groupId>
<artifactId>allure-testng</artifactId>
<version>2.13.8</version>
</dependency>
<!-- https://mvnrepository.com/artifact/io.github.bonigarcia/webdrivermanager -->
<dependency>
<groupId>io.github.bonigarcia</groupId>
<artifactId>webdrivermanager</artifactId>
<version>4.3.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.aspectj/aspectjweaver -->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.8.10</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-core -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.12.1</version>
</dependency>
</dependencies>
</project>
解决方法
您需要在测试用例中接受定义的参数作为参数,然后使用它们。
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;
import static org.junit.jupiter.api.Assertions.assertEquals;
public class StackOperationsTest {
static StackOperations obj = new StackOperations();
@ParameterizedTest(name = "#{index} - Test with String : {0}")
@CsvSource({
"100,TRUE","200,TRUE"
})
public void testPush(int number,boolean result) {
// Use number and result to verify if your logic is working or not.
Boolean bResult = obj.push(number);
assertEquals(result,bResult);
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。