0

I am performing unit testing (JUnit 4) and I am using assertEquals(), but I can't seem to find assertEquals(boolean, boolean).

Please tell me whether this is a generic method i.e. eclipse automatically adjusts to the type of argument being passed. For instance if I select assertEquals(double, double) and I pass string will it still work? If yes then is it because this is a generic method which operates on a wide range of data types and Eclipse automatically adjusts to the type of argument being passed.

I would also like to know (just for understanding purpose) what the graphical symbols mean inside the code assist window.

WarrenFaith
  • 57,492
  • 25
  • 134
  • 150
Apprentice
  • 21
  • 4
  • 2
    AssertTrue and assertFalse methods. Think in conditionals. – Brian Aug 06 '13 at 12:12
  • Eclipse doesn't really have to do anything with this, it's the realm of the compiler. If you use two `boolean`'s as the arguments the compiler will resolve the method signature to that, same for `double` and so on. – mikołak Aug 06 '13 at 12:12

5 Answers5

4

The code assist doesn't do anything beyond showing you what is available, and placing a "template" when you select a method. You can technically select anything you want, then pass different parameters (so long as a method with that signature exists). What matters is how the JVM will interpret your code.

I don't know whether you already have this, but place

import static org.junit.Assert.*;

at the top of your code, and you should see all the jUnit methods, including the boolean and String versions of assertEquals.

To your second question regarding code assist symbols, they relate to visibility, as follows:

  • Green circle: Public
  • Yellow diamond: Protected
  • Red Square: Private
  • Blue Triangle: Package
Paul Richter
  • 10,908
  • 10
  • 52
  • 85
1

If you want to check a boolean you can use assertTrue(yourBoolean); or assertFalse(yourBoolean); There is no (and no need for) assertEquals(boolean, boolean);

And yes it is generic and will adjust to the datatype if finds. Still if you want to compare doubles you can add a margin of error, meaning how close the doubles need to be to each other to be considered equal. assertEquals(expectedDouble, actualDouble, delta);

Akunosh
  • 237
  • 1
  • 2
  • 10
1

Refer to the JavaDoc for Assert.

And no, that method doesn't exist, but you can use assertTrue( boolean )/assertFalse( boolean ) instead.

Cheers,

Anders R. Bystrup
  • 15,729
  • 10
  • 59
  • 55
0

Try to use

assertTrue(boolean == boolean);
Grim
  • 1,938
  • 10
  • 56
  • 123
  • Why boolean == boolean, when you just have the boolean in question checked directly by the assertTrue or assertFalse? – Akunosh Aug 06 '13 at 12:40
  • Then what will the source of the expected boolean be, because it obviously can't have the same source as the checked one? Maybe i just lack an example where said boolean changed and you can read out somewhere else what you are expecting without being able to set this in specific testcases. If the assert fails i would not be sure whether it checkt with the wrong expectation or actually made a mistake. – Akunosh Aug 06 '13 at 13:16
0

This is overloading and has nothing to do with Eclipse. Eclipse just assist you as you type. So, even if you pick up assertEquals(double, double) and the actual parameters you passing to the method are, let's say, Objects, it will work. Picking up the method which takes doubles does not mean that you are forced to pass doubles. You can pass any type you want, as long as there is an overloaded method which accepts them.

loscuropresagio
  • 1,922
  • 15
  • 26