0
0
JUnittesting~10 mins

Argument conversion in JUnit - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to convert the string argument to an integer in the test method.

JUnit
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;

public class ConversionTest {
    @Test
    void testStringToInt() {
        String input = "123";
        int number = Integer.[1](input);
        assertEquals(123, number);
    }
}
Drag options to blanks, or click blank then click option'
AparseInt
BvalueOf
CtoString
DintValue
Attempts:
3 left
💡 Hint
Common Mistakes
Using valueOf returns an Integer object, not a primitive int.
Using toString is for converting to String, not from String.
intValue is a method on Integer objects, not static.
2fill in blank
medium

Complete the code to convert the string argument to a double in the test method.

JUnit
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;

public class ConversionTest {
    @Test
    void testStringToDouble() {
        String input = "3.14";
        double value = Double.[1](input);
        assertEquals(3.14, value);
    }
}
Drag options to blanks, or click blank then click option'
AdoubleValue
BvalueOf
CtoString
DparseDouble
Attempts:
3 left
💡 Hint
Common Mistakes
Using valueOf returns a Double object, not a primitive double.
toString is unrelated to conversion from String.
doubleValue is an instance method, not static.
3fill in blank
hard

Fix the error in the code to correctly convert the string argument to a boolean.

JUnit
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;

public class ConversionTest {
    @Test
    void testStringToBoolean() {
        String input = "true";
        boolean flag = Boolean.[1](input);
        assertTrue(flag);
    }
}
Drag options to blanks, or click blank then click option'
AparseBoolean
BvalueOf
CbooleanValue
DtoString
Attempts:
3 left
💡 Hint
Common Mistakes
Using valueOf returns a Boolean object, not primitive boolean.
booleanValue is an instance method, not static.
toString converts to String, not from String.
4fill in blank
hard

Fill both blanks to convert a string to an Integer object and then get its int value.

JUnit
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;

public class ConversionTest {
    @Test
    void testStringToIntValue() {
        String input = "456";
        Integer obj = Integer.[1](input);
        int number = obj.[2]();
        assertEquals(456, number);
    }
}
Drag options to blanks, or click blank then click option'
AvalueOf
BparseInt
CintValue
DtoString
Attempts:
3 left
💡 Hint
Common Mistakes
Using parseInt returns primitive int, not Integer object.
Using toString is unrelated to conversion.
Using parseInt then calling intValue causes error because int is primitive.
5fill in blank
hard

Fill all three blanks to convert a string to a Double object, get its double value, and assert equality.

JUnit
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;

public class ConversionTest {
    @Test
    void testStringToDoubleValue() {
        String input = "9.81";
        Double obj = Double.[1](input);
        double value = obj.[2]();
        assertEquals([3], value);
    }
}
Drag options to blanks, or click blank then click option'
AvalueOf
BdoubleValue
C9.81
DparseDouble
Attempts:
3 left
💡 Hint
Common Mistakes
Using parseDouble returns primitive double, not Double object.
Using toString is unrelated.
Using wrong literal in assertEquals causes test failure.