Close
« Tutorial

Spring MVC - @ActiveProfiles in Tests MCQ

0/13
0 ✓
0 ✗

1. Which annotation selects the dev profile for a test class?

@SpringJUnitWebConfig(AppConfig.class)
// Add annotation here
class GreetingTest {
    ...
}

2. What does @ActiveProfiles do in a Spring test class?

3. The controller returns the value of greeter.get() for a GET request to /. What is the response body when this test runs?

interface Greeter { String get(); }

@Service
@Profile("default")
class LocalGreeter implements Greeter {
    public String get() { return "local"; }
}

@Service
@Profile("dev")
class DevGreeter implements Greeter {
    public String get() { return "dev"; }
}

@Controller
class HomeController {
    @Autowired Greeter greeter;

    @GetMapping("/")
    @ResponseBody
    String handle() { return greeter.get(); }
}

@SpringJUnitWebConfig(AppConfig.class)
@ActiveProfiles("dev")
class HomeTest { ... }

4. The test class below has no @ActiveProfiles. What does greeter.get() return?

@Service
@Profile("default")
class LocalGreeter implements Greeter {
    public String get() { return "local"; }
}

@Service
@Profile("dev")
class DevGreeter implements Greeter {
    public String get() { return "dev"; }
}

@SpringJUnitWebConfig(AppConfig.class)
class GreeterTest {
    @Autowired Greeter greeter;
}

5. Which annotation activates both dev and cloud profiles in a test class?

6. A test class activates the dev profile. It also autowires a bean that exists only for the prod profile. What happens?

Join