What is a fixture?
Literally, the Fixture can be any precondition of a testing procedure or sets of steps that are used as postconditions.
Basically, It might be:
a set of objects used as a baseline for running tests
a set of initialization code functions
The purpose of using the Fixture is to eliminate the duplication of the common code for all the test cases
Different ways to define fixtures
In-class Fixture
Writing an in-class fixture when it’s supposed to be used only by this testing class. It should be defined in the companion object of the testing class
example:
class TestClass { @Test fun `test create loan - should return bad request if initialFeePercentage is negative`() { ... } private fun resolveCreateLoanRequestDtoV2( stakeholderId: UUID? = UUID.randomUUID(), principalAmount: BigDecimal? = BigDecimal("1000"), firstInstallmentDate: LocalDate? = LocalDate.now().plusMonths(1), numberOfInstallments: Int? = 6, interestRate: BigDecimal? = BigDecimal("0.025"), initialFee: BigDecimal? = BigDecimal("1.0"), initialFeePercentage: BigDecimal? = BigDecimal("0.01") ) = CreateLoanRequestDtoV2( stakeholderId!!, principalAmount!!, null, firstInstallmentDate!!, numberOfInstallments!!, interestRate!!, initialFee!!, initialFeePercentage!! ) }
Fixtures in the separate files
Writing a fixture in a shared separate file
Defining a data fixture
If it’s defining the data entities, It’s supposed to group the fixtures of the same entity in one file
example:
object LoanFixture { private val stakeholderId = UUID.fromString("be8af8cb-1107-4683-88d8-4c86f020ddea") private val loanId = UUID.fromString("bad4d498-56bc-4944-86b6-c5ce03634b6b") fun buildNewLoan( id:UUID? = loanId, customerId:UUID? = stakeholderId, emi:BigDecimal? = BigDecimal("100.00000") ) = LoanEntity( id = id!!, customerId = customerId!!, emi = emi!!) ... }
Defining a function fixture
If it’s defining the shared functions, it’s hard to apply one same rule. But it’s supposed to group the fixtures that belongs to the same domain.
example:
object ThoughMachineFixture { ... }