0

I'm learning to use Junit. I wrote a method which generates a number strictly less than xX.

I want to test it using Junit. Not sure which assertion to use as the expected is based on xX and I don't see any comparison assertions.

// within the Junit test class
ClassA a = new ClassA();

@Test
void randomTest(){
    assertEquals( ? ,a.getValue(5));
}


public int getValue(int xX){
    // returns an integer less than xX
    return (int) (Math.random() * xX);
}
Madplay
  • 1,027
  • 1
  • 13
  • 25
snhn0041
  • 3
  • 2
  • 2
    `assertTrue(a.getValue(5) < 5);` – 4castle Apr 25 '19 at 01:45
  • I can't look up the exact syntax, but you should look into hamcest matchers. `assertThat(a.getValue(5), Matchers.lessThan(5))`. If it falls, you'll get a nicer error message that assertTrue will give. – yshavit Apr 25 '19 at 02:03

1 Answers1

1

Use assertTrue like assertTrue(a.getValue(5)<5);

public static void assertTrue(boolean condition)

Asserts that a condition is true. If it isn't it throws an AssertionError without a message.

Ryuzaki L
  • 37,302
  • 12
  • 68
  • 98