-2

Why I can't have succeed to test this case?

@Test
public void test_Should_be_0() throws Exception {

    HSSFCell cell = new HSSFWorkbook().createSheet().createRow(0).createCell(0);
    cell.setCellValue(0);

    assertTrue(cell == 0);

Thank you for your help.

Nicolas Filotto
  • 43,537
  • 11
  • 94
  • 122
Lost_in_the_code
  • 75
  • 1
  • 1
  • 10

2 Answers2

1

Your test assertTrue(cell == 0) is not correct, indeed you try to compare an object's reference with 0 which cannot work as they are incompatible types, if you want to check whether the value of the cell is 0, you should rather do something like that:

assertEquals(0.0, cell.getNumericCellValue(), 0.0);
Nicolas Filotto
  • 43,537
  • 11
  • 94
  • 122
0

I think this one way to test the code:

1)

@Test
    public void test_Should_be_0() throws Exception {

        HSSFCell cell = new HSSFWorkbook().createSheet().createRow(0).createCell(0);
        cell.setCellValue(0);
        Assert.assertTrue(cell.getNumericCellValue() == 0);

or 2)

@Test
    public void test_Should_be_0() throws Exception {

        HSSFCell cell = new HSSFWorkbook().createSheet().createRow(0).createCell(0);
        cell.setCellValue(0);
        HSSFCell cell2 = new HSSFWorkbook().createSheet().createRow(0).createCell(0);
        cell2.setCellValue(0);
        Assert.assertTrue(cell.getNumericCellValue() == cell2.getNumericCellValue());

Because if the numbers are different the test doesn't work.

Lost_in_the_code
  • 75
  • 1
  • 1
  • 10
  • The second example is a bad idea. If you are trying to test `setCellValue()` or `getNumericCellValue()` and one is bad, it will always fail the same way given the same input. So if `cell.setCellValue(0)` puts a `9999` in the cell, `assertTrue(cell.getNumericCellValue() == 0)` should fail because it should return a `9999`. But if you set the value of two cells witn `setCellValue(0)` and then use `getNumericCellValue()` to compare the results, they should be equal and your assert in test 2 will always pass, even if the function are bad. – jmarkmurphy Dec 07 '16 at 13:56