Exam Questions and CORRECT Answers
segment code - CORRECT ANSWER - methods should perform one well-defined
functionality
how does segment code increase testability? - CORRECT ANSWER - more reusable and
maintainable, the methods don't depend on each other anymore
DRY - CORRECT ANSWER - Don't Repeat Yourself
DRYL why shouldn't we repeat ourselves? - CORRECT ANSWER - - Increased code to
maintain, more room for error
- Less timetable → have to do increased testing and when there is a defect, the bug fix has to be
replicated in all copies
DRY: what do i do with similar code? - CORRECT ANSWER - merge them!
functionally similar w/ diff types? - CORRECT ANSWER - polymorphism
DRY: which type of classes and methods should we make use of? - CORRECT
ANSWER - - generic ones
- parameterized types
use generic classes when - CORRECT ANSWER - there is no superclass
TUFs - CORRECT ANSWER - Test-Unfriendly Features
TUCs - CORRECT ANSWER - Test-Unfriendly Constructs
,no TUFs inside - CORRECT ANSWER - TUCs
TUFs are - CORRECT ANSWER - features that you want to fake using stubs (because
they take too long to set up to work correctly, or test, or testing them causes unwanted side-
effects
examples of TUFs - CORRECT ANSWER - - printing to console
- reading / writing from a database / to a filesystem
- access a diff program or system / the network
TUCs are - CORRECT ANSWER - methods that are hard to fake using stubbing or
overriding
stubbing def - CORRECT ANSWER - replacing a method in a mocked object using
Mockito
overriding def - CORRECT ANSWER - overriding a method in a "fake" class that
subclasses real class
TUCs examples - CORRECT ANSWER - - object constructors / destroctors
- private methods
- final methods
(the above four are impossible to override)
- static methods: impossible to override or to stub (since static methods are called on classes not
objects)
,so what does no TUFs inside TUCs mean? - CORRECT ANSWER - do not put code that
you want to fake (TUFs) inside methods that are hard to fake (TUCs)
make it easy to satisfy preconditions - CORRECT ANSWER - - depnednece on external
data is bad for testing
external data examples - CORRECT ANSWER - - val of global vars
- val extracted from a global data structure
- val read from a file / database
- basically any val that you did not pass in as args
- aka side-effects
pass in data using args (will need less external data) and for the remaining external data -
CORRECT ANSWER - - segregate hard-to-test code w/ side-effects into a small corner
- keep as many methods pure as possible
make it easy to reprouce - CORRECT ANSWER - - dependence on random data is bad for
testing
- random data = impossible to reproduce result
if you pass in Die d var, - CORRECT ANSWER - you can do
int dieRoll = d.roll();
(above is better)
than
int dieRoll = (new Die()).roll();
, with int dieRoll = d.roll(); you can - CORRECT ANSWER - mock Die and stub d.roll():
or you can just pass in dieRoll - CORRECT ANSWER - public Result playOverUnder(int
dieRoll) { if (dieRoll > 3) { return RESULT_OVER; } else { return RESULT_UNDER; } }
and don't have to mock or stub anything yay!
when we pass in instances of room into the house parameter, we can mock and stub more easily:
public class House {
private Room bedRoom;
private Room bathRoom;
public House(Room r1, Room r2) { bedRoom = r1;
bathRoom = r2;
}
public String toString() {
return bedRoom.toString() + " " + bathRoom.toString();
} } - CORRECT ANSWER - Room bedRoom = Mockito.mock(Room.class);
Room bathRoom = Mockito.mock(Room.class);
House house = new House(bedRoom, bathRoom);
dependency injection def - CORRECT ANSWER - passing a dependent object as arg
(instead of building it internally)
dependency injection features - CORRECT ANSWER - - makes testing easier by allowing
you to mock that object
- also what allowed us to mock the Die object for repproducibility
- has other software engineering benefits like decoupling the two classes