Search code examples
javaspringjunitjunit4

Java - JUnit how to do?


I have the following method and how can I do the JUnit testing on it?

Parser is my constructor, which I am also using as my return type of my following method.

As this method is splitting the string in three one, so for that I want to write a unit test case.

I am somehow familiar with JUnit but not that much. Any guidance/ link/ help will be appreciated.

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;
    }
}

I tried in my test:

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);

}

I got this error:

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)

Solution

  • try this

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