【问题标题】:Java - JUnit how to do?Java——JUnit怎么办?
【发布时间】:2017-07-24 20:13:29
【问题描述】:

我有以下方法,如何对其进行 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)

【问题讨论】:

  • 就像任何基本的 junit 测试用例一样:您使用已知输入调用该方法,然后检查输出是否符合您的预期,即具有预期的第一个和第二个值的新解析器。尝试一下。
  • 不要在 cmets 中发布代码。改为编辑您的问题。
  • 好的。我做到了。你现在可以检查了。
  • 您发布的不是有效的 Java。它甚至无法编译。
  • 你能再解释一下吗?怎么样?

标签: java spring junit junit4


【解决方案1】:

试试这个

@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());
    }

【讨论】:

    【解决方案2】:

    任何指导/链接/帮助将不胜感激。谢谢

    作为一种简单的方法,您可能想尝试以下几行中的某些内容。作为第一步,创建测试类:

    import org.junit.Test;
    import static org.junit.Assert.assertEquals;
    
    public class ExampleTest {
      Example example = new Example();
    
      @Test
      public void testThirdValueCleanup(){
        //pass some inputs to your function 
        //by exclicitly calling 
        //example.thirdValueCleanup(input params...);
    
        //a simple test case would be to check if it you get what your expect
        Parser expected = new Parser("set this one as it should be");
        assertEquals(example.thirdValueCleanup(some inputs...), expected);
      }
      /*
      * Here you can define more test cases
      */
    }
    

    接下来你可以创建一个类来运行你的测试:

    import org.junit.runner.JUnitCore;
    import org.junit.runner.Result;
    import org.junit.runner.notification.Failure;
    
    public class TestRunner {
       public static void main(String[] args) {
          Result result = JUnitCore.runClasses(ExampleTest.class);
    
          for (Failure failure : result.getFailures()) {
             System.out.println(failure.toString());
          }
    
          System.out.println(result.wasSuccessful());
       }
    }
    

    有关更多信息,您可以咨询this 教程或getting started by JUnit

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-03-18
      • 1970-01-01
      • 1970-01-01
      • 2012-12-23
      • 2012-08-30
      相关资源
      最近更新 更多