Computer scienceBackendSpring BootSpring Testing

Testing Beans

Application

Report a typo

Here is a component that acceses an external API to get random English words:

@Component
public class WordRandomizer {

    private RestTemplate restTemplate = new RestTemplate();

    public String getRandomWord() {
        // Accesses external API to get one random word
        return restTemplate.getForObject("https://random-word-api.vercel.app/api?words=1", String[].class)[0];
    }

    public List<String> get3RandomWords() {
        // Accesses external API to get 3 random words
        return restTemplate.getForObject("https://random-word-api.vercel.app/api?words=3", List.class);
    }

}

What values do the methods of wordRandomizer return in the test below?

@SpringBootTest
public class WordRandomizerTest {

    @MockBean
    public WordRandomizer wordRandomizer;

    @Test
    void shouldReturnDefaultValues() {
        var randomWordsList1 = wordRandomizer.get3RandomWords();
        Mockito.when(wordRandomizer.get3RandomWords()).thenReturn(List.of("mock1", "mock2", "mock3"));
        var randomWordsList2 = wordRandomizer.get3RandomWords();
        String randomWord = wordRandomizer.getRandomWord();
        System.out.println();
    }
}

Match each variable name with its value.

Match the items from left and right columns
randomWordsList1
randomWordsList2
randomWord
no such value
["some_random_word1", "some_random_word2", "some_random_word3"]
[]
["mock1", "mock2", "mock3"]
null
___

Create a free account to access the full topic