Mike's Blog

Spring Certification: Container, Dependency and IOC 2 | Reviewing the Study guide

By: Micheal Arsenault

Posted: December 25, 2020| Modified: December 26, 2020

Reading Time: <5 minutes

Tags: certification Java Spring

resources:


A lot of the study guide questions seem simple at the start but when you begin looking at the actual functionality you soon discover there’s lots of extra material that you can review.

Take the below question for example, there’s literally a single annotation needed to perform this task @ContextConfiguration; however, because of the additional testing framework required to be integrated you must also consider how the Spring Framework integrates with other technology. The good news is that Spring is notorious for bringing together different technologies into a comprehensive and sensible way.


How are you going to create an ApplicationContext in an integration test?


It’s relatively simple to create an ApplicationContext, Spring provides the annotation @ContextConfiguration that allows a test to use configuration metadata in order to load up an ApplicationContext.Example in the documentation, but THIS DOES NOT INCLUDE THE TESTING FRAMEWORK SETUP CODE.


SpringRunner source


JUnit 4 – Testing Doc

                                         // This indicates that we're using JUnit 4, but is just an Alias for 
@RunWith(SpringRunner.class)             // org.springframework.test.context.junit4.SpringJUnit4ClassRunner
@ContextConfiguration(classes = {AppConfig.class})      // Here we're loading up the configuration metadata from AppConfig.class
@ActiveProfiles(profiles = "test")      // This isn't required, but will ensure that Spring only loads up the beans under the "test" profile
public class ContextTesting {

    @Autowired      // Annotation provides autowiring of UserRepo bean
    UserRepo userRepo;

    @Autowired      // Annotation provides autowiring of Spring Environment bean
    Environment environment;

    @Test           // This will run the test and load up the Spring Application Context 
    public void contextLoads() {

    }
    ...
}

JUnit 5 | JUnit Jupiter – Testing Doc

@ExtendWith(SpringExtension.class)             // This indicates that we're using JUnit 5 / Jupiter with the SpringRunner.class
@ContextConfiguration(classes = {AppConfig.class})      // Here we're loading up the configuration metadata from AppConfig.class
@ActiveProfiles(profiles = "test")      // This isn't required, but will ensure that Spring only loads up the beans under the "test" profile
public class ContextTesting {

    @Autowired      // Annotation provides autowiring of UserRepo bean
    UserRepo userRepo;

    @Autowired      // Annotation provides autowiring of Spring Environment bean
    Environment environment;

    @Test           // This will run the test and load up the Spring Application Context 
    public void contextLoads() {

    }
    ...
}

@WebAppConfiguration is a class-level annotation that you can use to declare that the ApplicationContext loaded for an integration test should be a WebApplicationContext. The mere presence of @WebAppConfiguration on a test class ensures that a WebApplicationContext is loaded for the test, using the default value of "file:src/main/webapp" for the path to the root of the web application (that is, the resource base path). The resource base path is used behind the scenes to create a MockServletContext, which serves as the ServletContext for the test’s WebApplicationContext.

Spring Testing Documentation: @WebAppConfiguration link icon

  • This is a highly powerful annotation that helps mock out a web context so you can ensure proper testing is performed.
  • Baeldung has an article on it’s general usage as well.

Spring’s testing framework annotations provide support for the framework as well as for use cases. Here’s a short list of examples:

  • @ActiveProfiles does just what it says; you let Spring know which profile you’d like to use and you can use the beans for that profile. This is a great example of how Spring encourages you to test the logic of your code, because you wouldn’t want to hit a production database while you’re still working on a feature or a bug.

  • @DirtiesContext, sometimes when you’re doing test things in the context will become changed, and when that happens you don’t want it to affect your other tests so Spring provides an easy to use annotation, along with different ‘classMode’ setup to allow you to control this for yourself.

  • Additionally, there are several annotations related to sql and databse operations like commit and rollback.

    • With these you can help to control what you’re actually testing to ensure the results you’re expecting are what is true.