Thursday, April 25, 2024
HomeJavaPrime 20 Spring Framework Testing Interview Questions Solutions for two to five...

Prime 20 Spring Framework Testing Interview Questions Solutions for two to five Years Skilled


, I obtained quite a lot of emails so as to add extra interview questions, notably from
Testing and Spring Boot testing subjects the place I’ve seen quite a lot of curiosity
from the interviewers.  So, I made a decision to put in writing a separate article
containing spring boot testing interview questions. 


Since there may be quite a lot of overlap, I’ve mixed all spring boot
testing questions in a single article. This text accommodates all of the solutions to
Testing and Spring Boot Testing questions from the Spring certification
information in addition to some widespread Spring boot questions. 

Whereas lots of them are already widespread on Spring boot interviews they may
additionally enable you to be taught Spring boot higher and move the spring certification,
in case you are aiming to change into an authorized Spring Developer.

By the way in which, if you wish to be taught Spring Boot testing in-depth, I additionally
suggest becoming a member of a complete testing course like Testing Spring Boot: Newbie to Guru by John Thompson to be taught important Spring Boot testing
ideas. 

20+ Spring Framework and Spring Boot Testing Interview Questions
Solutions

If in case you have labored on Spring initiatives then you understand that we will use JUnit to
take a look at a Spring utility and this text will present extra lights on
use Spring Take a look at and Spring Boot options to check the interactions between
Spring and your code via widespread interview questions.

With none additional ado, right here is the record of incessantly requested Spring and
Spring Boot testing interview questions with solutions:

1. The right way to outline a testing class in Spring?
Listed here are the steps wanted to outline a testing class in Spring
utility:

  • annotate the take a look at class with
    @RunWith(SpringJUnit4ClassRunner.class)
  • annotate the category with
    @ContextConfiguration or
    @SpringBootTest with a view to
    inform the runner class the place the bean definitions come from
  • use @Autowired to inject
    beans to be examined.

That is all you could do to outline a testing class in spring. Tremendous straightforward?
is not it, that is why I like spring and Spring Boot. 


2. Which starter package deal do you could take a look at the spring boot
utility?

To unit take a look at the Spring Boot utility you will want to make use of the
spring-boot-starter-test, which imports each Spring Boot take a look at modules as
nicely as JUnit Jupiter, AssertJ, Hamcrest, and various different helpful
libraries. 

The spring-boot-starter-test makes use of
spring-boot-test and
spring-boot-test-autoconfigure.
You’ll be able to additional see Testing Spring Boot: Newbie to Guru for some reside examples of writing assessments utilizing the spring MVC take a look at and which
starter packages are robotically imported and which testing libraries they
robotically configure.  

spring boot testing questions for spring certificaiton

3. What sort of assessments usually use Spring?
Spring framework gives wonderful help for testing, you possibly can write each
unit assessments and integration assessments utilizing Spring and Spring Boot. A unit take a look at
is used to check particular person lessons like
controllers
whereas an integration take a look at can be utilized to check the interplay of a number of
models working collectively. Spring additionally gives mock objects for unit testing
and likewise present wonderful help for integration assessments on the
spring-test module.

4. What are the three widespread Spring boot take a look at annotations are?
Three widespread Spring boot take a look at annotations are
@DataJpaTest,
@WebMvcTest, and
@SpringBootTest which can be utilized
to check Spring Information JPA repository, Spring MVC utility, and Spring Boot,
lessons.

Spring Boot gives a
@SpringBootTest annotation, which
can be utilized as an alternative choice to the usual spring-test
@ContextConfiguration annotation
while you want Spring Boot options.

Spring Boot additionally gives
@MockBean annotation that may be
used to outline a Mockito mock for a bean inside our
ApplicationContext, which implies
the mock declared in take a look at class (through the use of
@MockBean) might be injected as a
bean inside the utility. 

A few of his classes are additionally free which I believe each Java developer ought to
undergo. 

5. How are you going to create a shared utility context in a JUnit integration
take a look at?

You’ll be able to create a shared utility context in a JUnit integration take a look at by
implementing the
ApplicationContextAware interface
as proven within the following instance:

@RunWith(SpringRunner.class)
@ContextConfiguration(lessons = BookConfiguration.class)
public class BookBorrowServiceJUnit4ContextTests implements ApplicationContextAware {
   
}

6. When and the place do you utilize @Transactional in testing?
If you happen to bear in mind, the
@Transactional annotation
defines the scope of a single database transaction and a database
transaction occurs contained in the scope of a persistence context. On testing,
you should use
@Transaction annotation at each
strategies and sophistication ranges.

On the technique degree, the annotated take a look at technique(s) will run, every in its
personal transaction. By default, robotically rolled again after completion of
the take a look at. You may also alter this habits by disabling the
defaultRollback attribute of
@TransactionConfiguration.

You may also use
@Transactional on the class
degree and in that case, every take a look at technique inside that class hierarchy runs
inside a transaction. 

You may also override this class-level rollback habits on the technique
degree utilizing the
@Rollback annotation, which
requires a boolean worth,
@Rollback(false).  

By the way in which, if you wish to be taught extra concerning the
@Transaction annotation in
Spring then you can even see the
Spring Framework for Learners with Spring Boot
course on Udemy to be taught extra about how and when to make use of
@Transactional annotation in
Spring Framework. 

Spring Framework Testing Interview Questions Answers for Java Developers

7. How is @ContextConfiguration utilized in Spring Boot?
The @ContextConfiguration is an
essential annotation for Spring Boot testing. It’s a class-level
annotation that’s used to load the appliance context for the
integration take a look at. That is outlined within the spring-test module.

Right here is an instance of
@ContextConfiguration in Spring
Boot assessments

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(lessons=BookConfiguration.class)
public class BookServiceTest {

  @Autowired 
  non-public BookService bookService;

  @Take a look at public void testBookService() {
   Ebook aBook = bookService.findByTitle("Spring Boot in Motion");
   assertEquals("Craig Partitions", aBook.getAuthor());
   assertEquals(40, aBook.getPrice());
  }


}

This instance, it makes use of
BookConfiguration class to
create an utility context and cargo bean definitions. if
@ContextConfiguration
is used with none attributes outlined, the default habits of spring
boot is to seek for a file named
{testClassName}-context.xml in
the identical location because the take a look at class and cargo bean definitions from there
if discovered.

Spring Boot additionally gives
@SpringBootTest annotation,
which can be utilized as an alternative choice to the usual spring-test
@ContextConfiguration
annotation while you want Spring Boot options.

The annotation works by creating the
ApplicationContext utilized in your
assessments via SpringApplication which not solely hundreds the appliance
context but additionally permits logging and loading of exterior properties
specified within the
utility.properties or
utility.yml file, and different options of the Spring Boot framework,
which isn’t loaded or enabled by the
@ContextConfiguration
annotation. 

Spring Boot + JUnit + Mockito Questions answers

8. How are mock frameworks corresponding to Mockito or EasyMock utilized in Spring
Boot?

You should utilize each Mockito and mock object frameworks like EasyMock
to put in writing assessments in Spring Boot. Mockito permits you to write assessments by
mocking the exterior dependencies with the specified habits whereas Mock
objects can be utilized to check particular situations.

You’ll be able to
use Mockito within the Spring boot utility utilizing both by @RunWith(MockitoJUnitRunner.class) to initialize the mock objects or through the use of MockitoAnnotations.initMocks(this) within the JUnit @Earlier than technique.

You may also use @MockBean annotation supplied by Spring Boot which can be utilized to outline a
new Mockito mock bean or substitute a Spring bean with a mock bean and inject
that into their dependent beans. One other advantage of that is that If
your take a look at makes use of one among Spring Boot’s take a look at annotations (corresponding to @SpringBootTest), this characteristic is robotically enabled.

9. How does Spring Boot simplify writing assessments?
Spring boot gives a starter dependency spring-boot-starter-test which
hundreds all-important testing libraries. 

For instance, the next libraries might be loaded inside the take a look at
scope:

  • JUnit: customary library for writing unit assessments in Java
  • JSON Path: XPath for JSON
  • AssertJ: Fluent assertion library
  • Mockito: Java mocking library
  • Hamcrest: Library of matcher objects
  • JSONassert: Assertion library for JSON
  • Spring Take a look at and Spring Boot Take a look at: These are the take a look at libraries
    supplied by the Spring Framework and Spring Boot.

Spring Boot additionally gives a
@SpringBootTest annotation,
which can be utilized as an alternative choice to the usual spring-test
@ContextConfiguration annotation while you want
Spring Boot options.


10. What does @SpringBootTest do? How does it work together with
@SpringBootApplication and @SpringBootConfiguration?

The @SpringBootTest annotation
tells Spring Boot to search for the primary configuration class (the one with
@SpringBootApplication, for
occasion) and use that to begin a Spring utility context.

11. When do you wish to use @SpringBootTest annotation?
You should utilize
@SpringBootTest annotation
everytime you wish to take a look at your Spring boot utility. For instance, you
can use @SpringBootTest to
confirm if the context is loaded or not and if the context is creating your
controller or not.

right here is an instance of utilizing
@SpringBootTest annotation to
verify

import static org.assertj.core.api.Assertions.assertThat;
import org.junit.jupiter.api.Take a look at;
import org.springframework.beans.manufacturing facility.annotation.Autowired;
import org.springframework.boot.take a look at.context.SpringBootTest;

@SpringBootTest
public class CheckControllerTest {
  @Autowired
  non-public BookController controller;
  
  @Take a look at
   public void contextLoads() throws Exception {
       assertThat(controller).isNotNull();
   }
}

You’ll be able to see we’re verifying if the controller is created or not
utilizing AssertJ strategies.

12. What does @SpringBootTest auto-configure?
Spring Boot gives the
@SpringBootTest annotation,
which can be utilized as an alternative choice to the usual spring-test
@ContextConfiguration
annotation while you want Spring Boot options. The annotation works by
creating the
ApplicationContext utilized in your
assessments via SpringApplication identical to
@SpringApplicaitonConfiguration
does. 


13. What dependencies does the spring-boot-starter-test brings to the
classpath?

The spring-boot-starter-test provides the next helpful testing libraries
into classpath:

  • JUnit 4: The de-facto customary for unit testing Java functions.
  • Spring Take a look at & Spring Boot Take a look at: Utilities and integration take a look at
    help for Spring Boot functions.
  • AssertJ: A fluent assertion library.
  • Hamcrest: A library of matcher objects (also called constraints or
    predicates).
  • Mockito: A Java mocking framework.
  • JSONassert: An assertion library for JSON.
  • JsonPath: XPath for JSON.

You’ll usually discover these widespread libraries to be helpful when writing
assessments.


14. How do you carry out integration testing with @SpringBootTest for a
net utility?

You’ll be able to both use
@SpringBootTest to load full
context for integration take a look at or you should use
@WebMvcTest to solely load the
net layer excited by loading the entire context.


15. When do you wish to use @WebMvcTest? What does it auto-configure?

You should utilize
@WebMvcTest annotation for
testing the net layer of your Spring Boot utility. Whenever you use
@WebMvcTest as an alternative of
@SpringBootTest then Spring
Boot solely instantiates the net layer relatively than the entire context.

In case your Spring Boot utility has a number of controllers then you possibly can even
instantiate a specific controller by specifying it as a parameter, for
instance,
@WebMvcTest(BookController.class)
will solely instantiate
BookController in your net
layer. 



16. What are the variations between @MockBean and @Mock annotations?

Each @Mock and
@MockBean are used extensively
whereas writing unit assessments in Java and Spring functions. It’s best to use
@Mock when unit testing your
enterprise logic impartial of the Spring framework, you are able to do this by
utilizing solely utilizing JUnit and Mockito and use
@MockBean while you write a take a look at
that’s backed by a Spring Take a look at Contexts and also you wish to add or substitute a
bean with a mocked model of it


17. When would you like @DataJpaTest for? What does it auto-configure?

You should utilize @DataJpaTest annotation to arrange an atmosphere with an
embedded database to check your database queries towards. That is one among
the handy options Spring boot gives to simplify testing. 

You should utilize @DataJpaTest to
take a look at Spring Information JPA repositories or every other JPA-related parts. All
you could do is simply add it you our unit take a look at and it’ll arrange a
Spring utility context:

Right here is an instance of
@DataJpaTest

annotation within the Spring Boot utility:

@ExtendWith(SpringExtension.class)
@DataJpaTest

public class OrderEntityRepositoryTest {

  @Autowired 
  non-public DataSource dataSource;
  @Autowired 
  non-public JdbcTemplate jdbcTemplate;
  @Autowired 
  non-public EntityManager entityManager;
  @Autowired 
  non-public OrderRepository orderRepository;


  @Take a look at
  public void injectedComponentsAreNotNull(){
   assertThat(dataSource).isNotNull();
   assertThat(jdbcTemplate).isNotNull();
   assertThat(entityManager).isNotNull();
   assertThat(orderRepository).isNotNull();
 }
}

Btw, these code examples use the
@ExtendWith annotation to inform
JUnit 5 to allow Spring help. however from Spring Boot 2.1, you do not want
to load the SpringExtension as a result of it is robotically included as a
meta-annotation within the Spring Boot take a look at annotations like
@DataJpaTest,
@WebMvcTest, and
@SpringBootTest



18. What’s using
@DirtiesContext annotation
whereas Testing Spring Boot utility?

One of many good options of Spring Take a look at help is that the appliance
context is cached between assessments. That method, when you’ve got a number of strategies in
a take a look at case or a number of take a look at instances with the identical configuration, they incur
the price of beginning the appliance solely as soon as. You’ll be able to management the cache
through the use of the
@DirtiesContext
annotation.


19. What’s the distinction between
@ContextConfiguration and
@SpringApplicatoinConfiguration
in Spring Boot testing?

Whereas each @ContextConfiguration and
@SpringApplicatoinConfiguration
annotations can be utilized to load utility context for spring boot
testing, there’s a refined distinction between them. 

Whereas
@ContextConfiguration hundreds
utility context it would not take full benefit of helpful Spring Boot
options. Spring Boot functions are in the end loaded by both
SpringApplication (within the case
of the JAR) or
SpringBootServletInitializer
(for net functions).

This class not solely hundreds the appliance context but additionally permits
logging and loading of exterior properties specified within the
utility.properties or
utility.yml file, and different
options of the Spring Boot framework, which isn’t loaded or enabled by
the
@ContextConfiguration
annotation.

Briefly, it is higher to make use of the
@SpringApplicatoinConfiguration
annotations relatively than
@ContextConfiguration for
writing an integration take a look at for the Spring boot utility.


20. What’s the distinction between @ContextConfiguration and
@SpringBootTest?

This query can be similar to the earlier query. In contrast to the
@ContextConfiguration which
simply hundreds spring beans and creates utility context for testing
with out utilizing spring boot options, the
@SpringBootTest annotation makes use of
SpringApplication behind the scenes to load ApplicationContext so that every one
the Spring Boot options might be out there.

That is all about
Spring Boot Testing Interview Questions and Solutions for Java
builders
. This text not solely covers spring boot-specific testing questions however
additionally common Spring framework testing questions. It additionally solutions all of the
testing-related questions that you’ll find within the official Spring
Certification Examination information, therefore in case you are making ready for spring
certification, you can even refer to those solutions to enhance your
information about this essential matter.

 Different Java and Spring articles you might like

  • 15 Spring Information JPA Interview Questions with solutions (questions)
  • 5 programs to be taught Spring Boot and Spring Cloud ( programs)
  • 15 Spring Cloud Interview Questions for Java builders (solutions)
  • 5 Programs to be taught Spring Cloud and Microservices (programs)
  • 15 Microservices Interview questions (solutions)
  • 3 Greatest Practices Java Programmers can be taught from Spring (greatest practices)
  • 5 Course to Grasp Spring Boot on-line (programs)
  • 10 Spring MVC annotations Java builders ought to be taught (annotations)
  • 10 Instruments Java Builders use of their day-to-day life (instruments)
  • 10 Programs to be taught Spring Safety with OAuth 2 (programs)
  • Prime 5 Books and Programs to be taught RESTful Net Service (books)
  • 5 Spring Boot Annotations for full-stack Java builders (tutorial)
  • 3 methods to vary Tomcat port in Spring Boot (tutorial)
  • Prime 5 Programs to be taught Microservices in Java? (programs)
  • 10 Superior Spring Boot Programs for Java Programmers (programs)

Thanks for studying this text thus far. If you happen to discover these
greatest Spring and Spring Boot Testing interview questions helpful then please share them with your pals and colleagues.
If in case you have any questions or suggestions then please drop a notice.

P. S. – If you wish to find out about Spring Boot and on the lookout for a
free Spring Boot on-line course then I additionally suggest you to affix
the Introducing Spring Boot (FREE ) course by Dan Vega on Udemy. It is top-of-the-line free
programs to be taught Spring Boot for Java builders. 



RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments