gpt4 book ai didi

Java-JUnit怎么办?

转载 作者:行者123 更新时间:2023-12-02 02:42:10 26 4
gpt4 key购买 nike

我有以下方法,如何对其进行 JUnit 测试?

Parser 是我的构造函数,我也将其用作以下方法的返回类型。

由于此方法将字符串分成三部分,因此我想编写一个单元测试用例。

我对 JUnit 有点熟悉,但不是那么熟悉。任何指导/链接/帮助将不胜感激。

public class Example {

public Parser thirdValueCleanup(String value) {
String thirdValueValue = value.trim();
System.out.println("TestData: "+thirdValueValue);
String firstValueRegex = "[A-z]\\d[A-z]";
String secondValueRegex = "\\d[A-z]\\d";
String thirdValueRegex = "[SK|sk]{2}";

Pattern firstValuePattern = Pattern.compile(".*\\W*("+firstValueRegex+")\\W*.*");
Pattern secondValuePattern = Pattern.compile(".*\\W*("+secondValueRegex+")\\W*.*");
Pattern thirdValuePattern = Pattern.compile(".*\\W*("+thirdValueRegex+")\\W*.*");

String firstValue = "";
String secondValue = "";
String thirdValue = "";

Matcher firstValueMatcher = firstValuePattern.matcher(thirdValueValue);
if(firstValueMatcher.matches()) {
firstValue = firstValueMatcher.group(1);
}

Matcher secondValueMatcher = secondValuePattern.matcher(thirdValueValue);
if(secondValueMatcher.matches()) {
secondValue = secondValueMatcher.group(1);
}

Matcher thirdValueMatcher = thirdValuePattern.matcher(thirdValueValue);
if(thirdValueMatcher.matches()) {
thirdValue = thirdValueMatcher.group(1);
}

String FirstValueName = firstValue + " " + secondValue;
String thirdValueName = thirdValue;

return new Parser(FirstValueName, thirdValueName);
}

public Parser(String firstValue, String secondValue) {
this.firstValue = firstValue;
this.secondValue = secondValue;
}

public String getFirstValue() {
return firstValue;
}

public String getSecondValue() {
return secondValue;
}
}

我在测试中尝试过:

public final void testThirdValueCleanup() {

System.out.println("retrieve");
String factories = "SK S6V 7L4";
Parser parser = new Parser();
Parser expResult = SK S6V 7L4;
Parser result = parser.thirdValueCleanup(factories);
assertEquals(expResult, result);

}

我收到此错误:

junit.framework.AssertionFailedError: expected:<SK S6V 7L4> but was:<com.example.Parser@379619aa>
at junit.framework.Assert.fail(Assert.java:57)
at junit.framework.Assert.failNotEquals(Assert.java:329)
at junit.framework.Assert.assertEquals(Assert.java:78)
at junit.framework.Assert.assertEquals(Assert.java:86)
at junit.framework.TestCase.assertEquals(TestCase.java:253)
at com.example.ParserTest.testthirdValueCleanup(ParserTest.java:29)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at junit.framework.TestCase.runTest(TestCase.java:176)
at junit.framework.TestCase.runBare(TestCase.java:141)
at junit.framework.TestResult$1.protect(TestResult.java:122)
at junit.framework.TestResult.runProtected(TestResult.java:142)
at junit.framework.TestResult.run(TestResult.java:125)
at junit.framework.TestCase.run(TestCase.java:129)
at junit.framework.TestSuite.runTest(TestSuite.java:252)
at junit.framework.TestSuite.run(TestSuite.java:247)
at org.junit.internal.runners.JUnit38ClassRunner.run(JUnit38ClassRunner.java:86)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)

最佳答案

试试这个

@Test
public final void testThirdValue() {

System.out.println("retrieve");
String factories = " BK M6B 7A4";
Parser parser = new Parser();
Parser province = parser.thirdValue(factories);

assertEquals("M6B 7A4", province.getFirstValue());
assertEquals("BK", province.getSecondValue());
}

关于Java-JUnit怎么办?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45289265/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com