CT Integration Tests - prasadtalasila/BITS-Darshini GitHub Wiki
Topic Outline
Unit Tests
- Spring Java configuration for each unit test
- classes with nested and autowired fields
- Mocks inside the Java configuration
- sample unit test class outline
- proper package structure for configuration
Integration Tests
- Autowiring of WebApp Context
Structure and Nomenclature
Dircectory Structure
All the unit tests written for BITS-Darshini are placed in the directory :
Whereas all the integration tests are in the directory :
Naming Convention
The package in which a test is placed follows the following convention in the src/test/java directory:
{type of test(unit/integration)}.{package structure of class to be tested}
for example, to write the unit test for a class in package in.ac.bits.protocolanalyzer.analyzer, we name the test package
unit.in.ac.bits.protocolanalyzer.analyzer
Guidelines for writing Spring tests
BITS-Darshini uses JUnit, Mockito and Hamcrest-Matchers for testing purposes.
There are two types of classes for testing in spring, the main test class to test the code, and the configuration class to provide configuration from external means for the test.
Test Class
They test the code for the application. Test methods are annotated with @Test
for the spring to identify them.
Annotations
Note that each test class is annotated with :
@RunWith(SpringJUnit4ClassRunner.class)
which indicates that the class should use Spring's JUnit facilities
And also :
@ContextConfiguration(classes = XYZConfig.class, loader = AnnotationConfigContextLoader.class)
which indicates the the class to be tested.
@Mock
Indicates the entity to be mocked.
@InjectMocks
Indicates the class in which to inject the mocked objects.
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
}
Initializes mock object(s) before every test.
@Autowired
Injects the bean created in config class of the test. For more info on bean injection, read this.
@Rule
We use this to specify the expected exception that will be thrown by an object during a test (Yes, we test the ability of an object to throw the desired exception too.)
Configuration Classes
Config classes enable us to centralize all the configuration and autowire beans in test class.
They are annotated with @Configuration annotation. The config file for the above sample test is placed in package :
unit.config.in.ac.bits.protocolanalyzer.analyzer (only change being addition of ".config" after unit)
The following annotation in configuration classes instructs spring to enable injection of beans of given class into the tests.
@Bean
Integration Tests
For integration test, we need to autowire "WebApplicationContext" and annotate the class with @WebAppConfiguration in order for spring to know the presence of integration tests.
Helpful Links
Below are some concise resources from which a great insight on spring testing can be gained :
Points to keep in mind
- RESTful API feature of BITS-Darshini might come in handy to obtain outputs of the certain test cases. See the wiki page for REST API here.