Java Interview Guide: How to Build Confidence With a Solid Understanding of Core Java Principles pdfdrive com



Yüklə 0,53 Mb.
Pdf görüntüsü
səhifə38/47
tarix06.05.2023
ölçüsü0,53 Mb.
#108601
1   ...   34   35   36   37   38   39   40   41   ...   47
Java Interview Guide

Unit Testing
Test-Driven Development
Test-driven development (TDD) encourages loosely coupled, highly cohesive,
and frequently tested modules of code. The TDD cycle begins by writing
an automated test case for functionality that doesn’t exist. Then, a developer
produces the minimum amount of code needed to make the test succeed.
As more and more test cases are added, a comprehensive test suite emerges that
can detect and prevent bugs caused by new functionality.
TDD can also be applied to bug fixing. Writing a test case before fixing a bug
will determine whether a fix was effective and at the same time guarantee that
the bug will never resurface.


Unit Tests
A unit test relies on assertions to verify the behavior of code. An assertion
throws an error if an expectation isn’t met, such as when a field doesn’t match
an expected value. A unit test succeeds if it completes without falling any
assertions or throwing any exceptions. Java provides the assert keyword,
but it relies on JVM arguments to enable and it’s preferable to use a framework
with more flexibility. JUnit is a popular unit testing framework that includes
a rich API for assertions as well as support for test fixtures.


Test Fixtures
A test fixture is a fixed state of objects used as a baseline for running tests.
For example, a test fixture could drop and recreate an in-memory database
to allow data access tests to work on a clean set of data after every
test execution.


Mock Objects
Mock objects mimic the behavior of objects that are too impractical
to incorporate into unit tests. For example, it would be prohibitive to charge
a credit card or launch a missile every time a unit test runs. Mock objects
implement the same interface as the objects they mimic, allowing developers
to simulate controlled scenarios without modifying the code that’s being tested.
Mock objects can be created manually, but frameworks such as Mockito provide
powerful proxies that can trace and verify the behavior of method calls.


Questions
What is test-driven development? Why is it beneficial?
What is a unit test?
What is JUnit?
What are text fixtures?
What are mock objects?



Yüklə 0,53 Mb.

Dostları ilə paylaş:
1   ...   34   35   36   37   38   39   40   41   ...   47




Verilənlər bazası müəlliflik hüququ ilə müdafiə olunur ©azkurs.org 2024
rəhbərliyinə müraciət

gir | qeydiyyatdan keç
    Ana səhifə


yükləyin