0
0
JUnittesting~10 mins

Stub objects 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 create a stub that returns a fixed value.

JUnit
StubClass stub = new StubClass() {
    @Override
    public int getValue() {
        return [1];
    }
};
Drag options to blanks, or click blank then click option'
A"value"
Btrue
Cnull
D10
Attempts:
3 left
💡 Hint
Common Mistakes
Returning a string instead of an int.
Returning null for a primitive int return type.
2fill in blank
medium

Complete the code to stub a method that returns a fixed string.

JUnit
StubClass stub = new StubClass() {
    @Override
    public String getName() {
        return [1];
    }
};
Drag options to blanks, or click blank then click option'
A100
Bnull
C"TestUser"
Dfalse
Attempts:
3 left
💡 Hint
Common Mistakes
Returning an integer instead of a String.
Returning false instead of a String.
3fill in blank
hard

Fix the error in the stub method to return the correct boolean value.

JUnit
StubClass stub = new StubClass() {
    @Override
    public boolean isAvailable() {
        return [1];
    }
};
Drag options to blanks, or click blank then click option'
Atrue
Bnull
C1
D"true"
Attempts:
3 left
💡 Hint
Common Mistakes
Returning "true" as a string instead of boolean true.
Returning 1 instead of a boolean.
4fill in blank
hard

Fill both blanks to stub a method that returns a list with one fixed element.

JUnit
StubClass stub = new StubClass() {
    @Override
    public List<String> getItems() {
        return [1];
    }
};

List<String> expected = new ArrayList<>();
expected.[2]("item1");
Drag options to blanks, or click blank then click option'
Anew ArrayList<>()
Badd
Cremove
Dclear
Attempts:
3 left
💡 Hint
Common Mistakes
Using remove or clear instead of add.
Returning null instead of a new list.
5fill in blank
hard

Fill all three blanks to stub a method that returns a map with one key-value pair.

JUnit
StubClass stub = new StubClass() {
    @Override
    public Map<String, Integer> getScores() {
        Map<String, Integer> map = [1];
        map.[2]([3], 100);
        return map;
    }
};
Drag options to blanks, or click blank then click option'
Anew HashMap<>()
Bput
C"player1"
Dclear
Attempts:
3 left
💡 Hint
Common Mistakes
Using clear instead of put.
Using an unquoted key for the map.