This is first and primary session article about JUnit testing tutorial for beginners. This is really useful article to start your work.
JUnit testing tutorial for beginners
- Why testing?
- Overview of JUnit’s role in testing
- Using JUnit within Eclipse
- Junit Test methods
- Assertions in TestCase
- Testing for exceptions
- Creating and organizing test fixtures with setUp/tearDown and
- Organizing Tests and Test Suites
- Executing Tests
- Integration with Maven
- Parameterized Tests
- Testing with Mockito
- Designing Java code with testing in mind
- To discover defects early in software during its development phases. 2. To ensure that product works as per specification.
- To avoid end user detecting problems
- To ensure customers satisfaction and reliability of the application.
- To stay in business
Types of Tests
- Tests the smallest unit of functionality, typically a method/function
- Unit tests should be fast to get quick feedback about reliability of software, whenever software is compiled.
- Any kind of dependency that is slow should be stubbed/mocked using the appropriate techniques so you can focus on what the unit of code is doing, not what its dependencies do.
- Unit tests allow us to change the code quickly and verify the change faster.
- Unit tests drive your design. You write a test, add code to fix the test, refactor code with confidence, and apply the design. This results in a simple, clean, maintainable, loosely coupled, and cohesive design.
- Verify communication paths and interactions between components to detect interface defects.
- Integration tests build on unit tests by combining the units of code and testing that the resulting combination functions correctly.
- Integration tests access database, external systems like email servers etc. to ensure that the code work correctly in different environments.
Overview of JUnit’s role in testing
Advantages of using Junit framework:
- Simple to use
- Provides Assertions for testing expected results
- Provides Test fixtures for sharing common test data
- Provides Graphical and textual test runners
- Junit shows test progress in a bar that is green if test is going fine and it turns red when a test fails.
- JUnit Framework can be easily integrated with IDE like Eclipse and build tools like Ant and Maven
- Can be integrated in CI systems.