Chapter 6, Spring Testing

Q1. What is the difference between JUnit4 and TestNG?

JUnit and TestNG, both are unit testing frameworks, which look very similar in functionality. Both provide functionalities such as annotation supports, exception test, timeout test, ignore test, and suite test. Whereas, a group test and dependency test is only supported by TestNG. TestNG has the ability to dynamically generate the test data for parameterized test, whereas JUnit cannot. The following is a list of few annotations supported by TestNG and JUnit4:

Feature

TestNG

JUnit4

Test annotation

@Test

@Test

Before the first test method in the current class

@BeforeClass

@BeforeClass

After all the test methods in the current class

@AfterClass

@AfterClass

Before each test method

@BeforeMethod

@Before

After each test method

@AfterMethod

@After

Before all tests in this suite run

@BeforeSuite

-

After all tests in this suite run

@AfterSuite

-

Run before the test

@BeforeTest

-

Run after the test

@AfterTest

-

Q2. What is the difference between unit testing and integration testing?

Unit testing, as the name suggests, is testing of every individual method of the code. It is the method of testing fundamental pieces of your functionality. It is a piece of code written by a software developer to test a specific functionality in the code. Unit tests are more about improving quality and preventing bugs, less about finding them, and are automated using testing frameworks.

Integration testing is the phase of software testing in which individual software modules are combined and tested as a group to ensure that required units are properly integrated and interacted with each other correctly. The purpose of integration testing is to verify functional, performance, and reliability of the code. The integration testing is used to test several units altogether.

Q3. Explain the Spring MVC test Framework.

Spring MVC test framework makes unit testing and integration testing of Spring MVC controller more meaningful by offering first class JUnit support. It helps in testing all aspects of controller method, which has not tested before. It allows us to perform testing in depth without starting a web container.

In order to perform a test of Spring MVC, the Spring TestContext framework, along with JUnit or TestNG, make it simple by providing annotation driven unit and integration testing support. The Spring TestContext framework can be used using @RunWith, @WebAppConfiguration, and @ContextConfiguration annotation to load Spring configuration, and inject the WebApplicationContext to the MockMvc for unit and integration test.

Q4. Explain @ContextConfiguration and @WebAppConfiguration.

The @ContextConfiguration annotation is used to set the ApplicationContext for test classes, by taking the actual configuration file with the file path. In the following code, we have given the file, so it will take relative path as the root package. We can also give the exact path by specifying the file: prefix. The @ContextConfiguration caches the ApplicationContext for us, and puts it in a static memory for the entire duration of the test or the test suite. The entire tests executes in the same JVM because of ApplicationContext stored in the static memory. If the second JVM is there, it will not have access to the static context, and will result in second ApplicationContext to be created.

The @WebAppConfiguration annotation is a class-level annotation used to create a web version of the application context in Spring. It is used to denote that the ApplicationContext, which is loaded for an integration test and used by that class, is an instance a WebApplicationContext. It is important to note that the @WebAppConfiguration annotation must be used together with @ContextConfiguration.

Q5. Explain MockMvc and @RunWith(SpringJUnit4ClassRunner.class).

The MockMvc is a key part of Spring MVC Test framework, which can be used to write tests for applications developed using Spring MVC. It is the entry point for Spring MVC Testing. The MockMvc mock the entire Spring MVC infrastructure and is created by using the implementations of the MockMvcBuilder interface. In order to use Spring MVC testing, the first step is to create an instance of MockMvc.

The @RunWith annotation is a JUnit annotation. It executes the tests in a class annotated with the @RunWith annotation, or extends a class annotated with the @RunWith annotation by invoking the class passed as the parameter, which means that the tests in annotated class are not executed by the in-built API in the JUnit framework, the runner class used to execute the test case. In order to use Spring's JUnit class runner for running test cases within Spring's ApplicationContext environment passed Spring's SpringJUnit4ClassRunner class as a parameter.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.147.47.59