Home » Spring

Spring

Last Updated on July 9, 2023 by KnownSense

41. Differentiate between WebClient and WebTestClient?
The difference between the Web client and Webtestclient can be stated as follows.

Web clientWebtestclient
Web client acts as a reactive client who performs non-blocking HTTP requests. Webtestclient also acts as a reactive client that can be used in tests.
It can handle reactive streams with backpressure.It can bind directly to WebFlux application by applying mock request and response objects.
It can take advantage of JAVA 8 Lambdas.It can connect to any server over an HTTP connection.

42. Does SpringBoot allows Spring MVC and Webflux in the same app?
Yes, Spring Boot can allow either Spring MVC or Spring WebFlux in the same application but with the condition to apply only one at a time. This is because MVC is a blocking paradigm, and WebFlux is a non-blocking paradigm and hence cannot be used together.

43. How to handle Exceptions in Spring framework?
Spring MVC Framework provides the following ways to help us achieving robust exception handling.

Controller-Based – We can define exception handler methods in our controller classes. All we need is to annotate these methods with @ExceptionHandler annotation.

Global Exception Handler – Exception Handling is a cross-cutting concern and Spring provides @ControllerAdvice annotation that we can use with any class to define our global exception handler.

HandlerExceptionResolver implementation – For generic exceptions, most of the time we serve static pages. Spring Framework provides a HandlerExceptionResolver interface that we can implement to create a global exception handler. The reason behind this additional way to define the global exception handler is that the Spring framework also provides default implementation classes that we can define in our spring bean configuration file to get spring framework exception handling benefits.

44. What is ContextLoadListener?
ContextLoaderListener is the listener class used to load root context and define spring bean configurations that will be visible to all other contexts. It’s configured in web.xml file as:
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/root-context.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

45. What are min. config needed to create a Spring MVC app?
For creating a simple Spring MVC application, we would need to do the following tasks.

  • Add spring-context and spring-webmvc dependencies in the project.
  • Configure DispatcherServlet in the web.xml file to handle requests through the spring container.
  • Spring bean configuration file to define beans, if using annotations then it has to be configured here. Also, we need to configure the view resolver for view pages.
  • Controller class with request mappings defined to handle the client requests.

46. Difference between Mockito.mock() vs @Mock vs @MockBean
The Mockito.mock() method allows us to create a mock object of a class or an interface. We can then use the mock to stub return values for its methods and verify if they were called.
@Test
public void mockExample() {
UserRepository localMockRepository = Mockito.mock(UserRepository.class);
Mockito.when(localMockRepository.count()).thenReturn(111L);
}

@Mock is shorthand for the Mockito.mock() method. It’s important to note that we should only use it in a test class. Unlike the mock() method, we need to enable Mockito annotations to use this annotation.
@Mock
UserRepository mockRepository;
@Test
public void mockExample() {
Mockito.when(mockRepository.count()).thenReturn(123L);
long userCount = mockRepository.count();
}

@MockBean use to add mock objects to the Spring application context. The mock will replace any existing bean of the same type in the application context. This annotation is useful in integration tests where a particular bean, like an external service, needs to be mocked.

47. Explain @SpyBean
Spy wraps the real bean but allows you to verify method invocation and mock individual methods without affecting any other method of the real bean. So, by making MongoTemplate a SpyBean we can mock only the methods we want to mock in our test case and leave the others untouched.

48. Difference between @MockBean and @SpyBean
When we use @MockBean for an object, it mocks the object and all its methods with do nothing and their result value will be null or we can define some custom behavior for its methods.
But, with @SpyBean object, object will behave like an @autowired object and all its methods will actually works, but we can define some custom behavior for its methods (via doReturn(…))


Authored by codingknownsense.com

Scroll to Top