Clean Code



Download 3,58 Mb.
Pdf ko'rish
bet136/384
Sana05.04.2022
Hajmi3,58 Mb.
#530298
1   ...   132   133   134   135   136   137   138   139   ...   384
Bog'liq
Clean Code

Listing 9-6 
MockControlHardware.java
public String getState() {
String state = "";
state += heater ? "H" : "h";
state += blower ? "B" : "b";
state += cooler ? "C" : "c";
state += hiTempAlarm ? "H" : "h";
state += loTempAlarm ? "L" : "l";
return state;
}


130
Chapter 9: Unit Tests
That is the nature of the dual standard. There are things that you might never do in a
production environment that are perfectly fine in a test environment. Usually they involve
issues of memory or CPU efficiency. But they 
never
involve issues of cleanliness. 
One Assert per Test
There is a school of thought
4
that says that every test function in a JUnit test should have one
and only one assert statement. This rule may seem draconian, but the advantage can be seen
in Listing 9-5. Those tests come to a single conclusion that is quick and easy to understand. 
But what about Listing 9-2? It seems unreasonable that we could somehow easily
merge the assertion that the output is XML and that it contains certain substrings. How-
ever, we can break the test into two separate tests, each with its own particular assertion, as
shown in Listing 9-7.
Notice that I have changed the names of the functions to use the common given-when-
then
5
convention. This makes the tests even easier to read. Unfortunately, splitting the tests
as shown results in a lot of duplicate code.
We can eliminate the duplication by using the T
EMPLATE
M
ETHOD
6
pattern and putting
the
given/when
parts in the base class, and the 
then
parts in different derivatives. Or we could
create a completely separate test class and put the 
given
and
when
Download 3,58 Mb.

Do'stlaringiz bilan baham:
1   ...   132   133   134   135   136   137   138   139   ...   384




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©hozir.org 2024
ma'muriyatiga murojaat qiling

kiriting | ro'yxatdan o'tish
    Bosh sahifa
юртда тантана
Боғда битган
Бугун юртда
Эшитганлар жилманглар
Эшитмадим деманглар
битган бодомлар
Yangiariq tumani
qitish marakazi
Raqamli texnologiyalar
ilishida muhokamadan
tasdiqqa tavsiya
tavsiya etilgan
iqtisodiyot kafedrasi
steiermarkischen landesregierung
asarlaringizni yuboring
o'zingizning asarlaringizni
Iltimos faqat
faqat o'zingizning
steierm rkischen
landesregierung fachabteilung
rkischen landesregierung
hamshira loyihasi
loyihasi mavsum
faolyatining oqibatlari
asosiy adabiyotlar
fakulteti ahborot
ahborot havfsizligi
havfsizligi kafedrasi
fanidan bo’yicha
fakulteti iqtisodiyot
boshqaruv fakulteti
chiqarishda boshqaruv
ishlab chiqarishda
iqtisodiyot fakultet
multiservis tarmoqlari
fanidan asosiy
Uzbek fanidan
mavzulari potok
asosidagi multiservis
'aliyyil a'ziym
billahil 'aliyyil
illaa billahil
quvvata illaa
falah' deganida
Kompyuter savodxonligi
bo’yicha mustaqil
'alal falah'
Hayya 'alal
'alas soloh
Hayya 'alas
mavsum boyicha


yuklab olish