Question: How do I use this: Exception ex = assertThrows ( RuntimeException . class, ( ) - > Project 2 . squareRoot ( - 4 )

How do I use this:
Exception ex = assertThrows(RuntimeException.class, ()-> Project2.squareRoot(-4));
to test that the method throws an exception because negative square roots arent allowed.
Use the assertThrows() JUnit assertion that takes the class of the expected exception and a lambda expression to run that should throw.
My test method is:
@Test
public void testNegativeCubeRoot(){
// Test cases for cubeRoot
assertEquals(2, Project2.cubeRoot(-8));
assertEquals(3, Project2.cubeRoot(-27));
assertEquals(4, Project2.cubeRoot(-64));
assertEquals(2, Project2.cubeRoot(-9));
assertEquals(1, Project2.cubeRoot(-1));
}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!