The Global Insight.

Informed perspectives on world events and diverse topics

business

What is @ContextConfiguration

By Emily Schmidt

@ContextConfiguration defines class-level metadata that is used to determine how to load and configure an ApplicationContext for integration tests.

What is @SpringApplicationConfiguration?

@ContextConfiguration is an annotation from the Spring Test Framework, which is suitable for every Spring application, @SpringApplicationConfiguration is from Spring Boot and is actually a composite annotation, which includes ContextConfiguration with the custom SpringApplicationContextLoader as loader.

What is AnnotationConfigContextLoader?

public class AnnotationConfigContextLoader extends AbstractGenericContextLoader. Concrete implementation of AbstractGenericContextLoader that loads bean definitions from component classes. See the Javadoc for @ContextConfiguration for a definition of component class.

What is @WebAppConfiguration?

@WebAppConfiguration is a class-level annotation that is used to declare that the ApplicationContext loaded for an integration test should be a WebApplicationContext . … This annotation may be used as a meta-annotation to create custom composed annotations.

What does spring boot test do?

Spring Boot provides a number of utilities and annotations to help when testing your application. Test support is provided by two modules: spring-boot-test contains core items, and spring-boot-test-autoconfigure supports auto-configuration for tests.

Why do we need @ContextConfiguration?

@ContextConfiguration loads an ApplicationContext for Spring integration test. @ContextConfiguration can load ApplicationContext using XML resource or the JavaConfig annotated with @Configuration . The @ContextConfiguration annotation can also load a component annotated with @Component , @Service , @Repository etc.

Why do we use @ContextConfiguration?

The @RunWith annotation is required to enable Spring integration testing and that’s why we have passed the SpringJUnit4ClassRunnner class to it, while @ContextConfiguration annotation specifies how to load application context.

What is EnableWebMvc?

The @EnableWebMvc annotation is used for enabling Spring MVC in an application and works by importing the Spring MVC Configuration from WebMvcConfigurationSupport. The XML equivalent with similar functionality is <mvc:annotation-driven/>.

What is WebMvcTest?

@WebMvcTest annotation is used for Spring MVC tests. It disables full auto-configuration and instead apply only configuration relevant to MVC tests. The WebMvcTest annotation auto-configure MockMvc instance as well.

What is MockMvcBuilders standaloneSetup?

We set up the MockMvc . We add the MyController to the standalone setup. The MockMvcBuilders. standaloneSetup() allows to register one or more controllers without the need to use the full WebApplicationContext .

Article first time published on

What is MockMvc used for?

MockMvc provides support for Spring MVC testing. It encapsulates all web application beans and makes them available for testing.

What is bean in spring?

A bean is an object that is instantiated, assembled, and otherwise managed by a Spring IoC container. Otherwise, a bean is simply one of many objects in your application. Beans, and the dependencies among them, are reflected in the configuration metadata used by a container.

What is a spring boot?

Java Spring Boot (Spring Boot) is a tool that makes developing web application and microservices with Spring Framework faster and easier through three core capabilities: Autoconfiguration. An opinionated approach to configuration. The ability to create standalone applications.

What is @EnableAutoConfiguration?

The @EnableAutoConfiguration annotation enables Spring Boot to auto-configure the application context. Therefore, it automatically creates and registers beans based on both the included jar files in the classpath and the beans defined by us.

What is the difference between SpringJUnit4ClassRunner and SpringRunner?

There is no difference, from the javadoc: SpringRunner is an alias for the SpringJUnit4ClassRunner. @RunWith(SpringRunner. class) tells JUnit to run using Spring’s testing support.

What is the use of @SpringBootApplication?

Spring Boot @SpringBootApplication annotation is used to mark a configuration class that declares one or more @Bean methods and also triggers auto-configuration and component scanning. It’s same as declaring a class with @Configuration, @EnableAutoConfiguration and @ComponentScan annotations.

What is the purpose of assertArrayEquals message AB?

7. What is the purpose of assertArrayEquals(“message”, A, B)? Explanation: Asserts the equality of the A and B arrays. The “message” is displayed to the user.

What is application context in spring?

The Application Context is Spring’s advanced container. Similar to BeanFactory, it can load bean definitions, wire beans together, and dispense beans upon request. … BeanFactory can still be used for lightweight applications like mobile devices or applet-based applications.

How do I use a component scan?

Using @ComponentScan in a Spring Application. With Spring, we use the @ComponentScan annotation along with the @Configuration annotation to specify the packages that we want to be scanned. @ComponentScan without arguments tells Spring to scan the current package and all of its sub-packages.

What is MockHttpServletRequestBuilder?

public class MockHttpServletRequestBuilder extends Object implements ConfigurableSmartRequestBuilder<MockHttpServletRequestBuilder>, Mergeable. Default builder for MockHttpServletRequest required as input to perform requests in MockMvc .

What is @MockBean?

@MockBean annotation It allow to mock a class or an interface and to record and verify behaviors on it. It can be used as a class level annotation or on fields in either @Configuration classes, or test classes that are @RunWith the SpringRunner. … @MockBean is similar to mockito’s @Mock but with Spring support.

What is ExtendWith?

@ExtendWith is a repeatable annotation that is used to register extensions for the annotated test class or test method.

What is EnableAspectJAutoProxy?

Annotation Type EnableAspectJAutoProxy Users can control the type of proxy that gets created for FooService using the proxyTargetClass() attribute. The following enables CGLIB-style ‘subclass’ proxies as opposed to the default interface-based JDK proxy approach.

What is @EnableAsync?

EnableAsync is used for configuration and enable Spring’s asynchronous method execution capability, it should not be put on your Service or Component class, it should be put on your Configuration class like: @Configuration @EnableAsync public class AppConfig { }

What is @ComponentScan in Spring?

Simply put – @ComponentScan tells Spring in which packages you have annotated classes which should be managed by Spring. So, for example, if you have a class annotated with @Controller which is in a package which is not scanned by Spring, you will not be able to use it as Spring controller.

What is MvcResult?

public interface MvcResult. Provides access to the result of an executed request. Since: 3.2 Author: Rossen Stoyanchev.

What is webAppContextSetup?

public static DefaultMockMvcBuilder webAppContextSetup(WebApplicationContext context) Build a MockMvc instance using the given, fully initialized (i.e., refreshed) WebApplicationContext . The DispatcherServlet will use the context to discover Spring MVC infrastructure and application controllers in it.

What is RunWith SpringJUnit4ClassRunner?

@RunWith(SpringJUnit4ClassRunner. class): Indicates that the class should use Spring’s JUnit facilities. … If you are using annotations rather than XML files, then any class that you are unit testing that requires Spring dependency injection needs to be put into the @ContextConfiguration annotation.

Is MockMvc integration test?

Technically speaking, tests using MockMvc are in the boundaries between unit and integration tests. They aren’t unit tests because endpoints are tested in integration with a Mocked MVC container with mocked inputs and dependencies.

How do you do integration testing?

  1. Write a test plan.
  2. Create test cases and use cases.
  3. Run tests after unit integration.
  4. Detect errors.
  5. Retest the functionality after bug fixing.
  6. Repeat the testing cycle until all bugs are fixed.

Why is integration test case used?

The purpose of the integration testing is to expose faults in the interaction between integrated units. Once all the modules have been unit tested, integration testing is performed.