0

I have test cases defined in an Excel sheet. I am reading a string from this sheet (my expected result) and comparing it to a result I read from a database (my actual result). I then use AssertEquals(expectedResult, actualResult) which prints any errors to a log file (i'm using log4j), e.g. I get java.lang.AssertionError: Different output expected:<10> but was:<7> as a result.

I now need to write that result into the Excel sheet (the one that defines the test cases). If only AssertEquals returned String, with the AssertionError text that would be great, as I could just write that immediately to my Excel sheet. Since it returns void though I got stuck.

Is there a way I could read the AssertionError without parsing the log file?

Thanks.

Dragonfly
  • 217
  • 1
  • 2
  • 15

4 Answers4

2

I think you're using junit incorrectly here. THis is why

  • assertEquals not AssertEquals ( ;) )
  • you shouldnt need to log. You should just let the assertions do their job. If it's all green then you're good and you dont need to check a log. If you get blue or red (eclipse colours :)) then you have problems to look at. Blue is failure which means that your assertions are wrong. For example you get 7 but expect 10. Red means error. You have a null pointer or some other exception that is throwing while you are running
  • You should need to read from an excel file or databse for the unit tests. If you really need to coordinate with other systems then you should try and stub or mock them. With the unit test you should work on trying to testing the method in code
  • if you are bootstrapping on JUnit to try and compare an excel sheet and database then I would ust export the table in excel as well and then just do a comparison in excel between columns
Community
  • 1
  • 1
RNJ
  • 15,272
  • 18
  • 86
  • 131
  • Hi RNJ, thanks for your response. What I am doing is testing many combinations for a particular system (e.g. a particular test could have 10,000 combinations of different parameter values, i.e. 10,000 rows in my Excel sheet). Therefore I have a one test method (annotated with @Test) that contains a loop, and after some processing I pass the expected value (retrieved from my Excel test cases) and the actual value (retrieved from my database) to assertEquals. I then write the result of 'PASS' or 'FAIL' back to my Excel sheet for each row. This works fine and serves it purpose well. – Dragonfly Nov 17 '15 at 10:35
  • What I'd like now is to simply retrieve the String result of assertEquals to write that also into my Excel sheet, so that for each test case, besides PASS or FAIL, I can also see the assertEquals result which would be great for my users ... – Dragonfly Nov 17 '15 at 10:40
1

Reading from/writing to files is not really what tests should be doing. The input for the tests should be defined in the test, not in the external file which can change - this can either introduce false negatives or even worse false positives (making your tests effectively useless while also giving false confidence that everything is ok because tests are green).

Given your comment (a loop with 10k different parameters coming from file), I would recommend converting this excel file into JUnit Parameterized test. You may want to put the array definition in another class, because 10k lines is quite a lot.

If it is some corporate bureaucracy, and you need to have this excel file, then it makes sense to not write a classic "test". I would recommend just a main method that does the job - reads the file, runs the code, checks the output using simple if (output.equals(expected)) and then writes back to file.

Jaroslaw Pawlak
  • 5,538
  • 7
  • 30
  • 57
1

Wrap your AssertEquals(expectedResult, actualResult) with try catch in catch

catch(AssertionError e){
 //deal with e.getMessage or etc.
}

But it not good idea for some reasons, I guess.

And try google something like soft assert

Renat
  • 81
  • 6
1

Documentation on assertEquals is pretty clear on what the method does:

Asserts that two objects are equal. If they are not, an AssertionError without a message is thrown.

You need to wrap the assertion with try-catch block and in the exception handling do Your logging. You will need to make Your own message using the information from the specific test case, but this what You asked for.


Note:

If expected and actual are null, they are considered equal.

Aleksandar
  • 3,558
  • 1
  • 39
  • 42