igor.wojda
02/27/2023, 1:42 PMdata class Task(val name: String, val description: String)
class CreateTeskUseCase(val repository: Repository) {
fun execute(taskRequestModel: TaskRequestModel) {
val task = Task(taskRequestModel.name, taskRequestModel.description)
//...
repository.save(task)
}
}
This works fine, but when I want to test interaction with the repo the "dummy" factory is required to get testable Task
instance:
class CreateTeskUseCase(val repository: Repository, val taskFactory: TaskFactory) {
fun execute(taskRequestModel: TaskRequestModel) {
val task = taskFactory.create(taskRequestModel.name, taskRequestModel.description)
//...
repository.save(task)
}
}
class TaskFactory {
fun create(name: String, description: String) = Task(name, description)
}
I wonder if there is a way to generate this TaskFactory
class (perhaps using ksp
). Something like this @GenerateFactory
annotation?
@GenerateFactory
data class Task(val name: String, val description: String)
Sam
02/27/2023, 1:43 PMTaskFactory
is a functional interface, you should be able pass ::Task
as the instance. Does that help?igor.wojda
02/27/2023, 1:45 PMSam
02/27/2023, 1:46 PMfun interface TaskFactory {
fun create(name: String, description: String): Task
}
data class Task(val name: String, val description: String)
val factory: TaskFactory = ::Task
But I guess SAM conversion doesn’t work for function referencesfun interface TaskFactory {
fun create(name: String, description: String): Task
}
data class Task(val name: String, val description: String)
val factory = TaskFactory(::Task)
igor.wojda
02/27/2023, 4:06 PMkqr
02/28/2023, 7:26 AMigor.wojda
02/28/2023, 8:27 AMrepository.save(task)
) eg. that name
String was not accidentally switched with description
string. How would you test this?kqr
02/28/2023, 12:43 PMigor.wojda
03/02/2023, 8:43 AMTim Hill
03/02/2023, 6:44 PMigor.wojda
03/02/2023, 9:32 PMCreateTaskUseCase
serves as a factory, so this basically is the same as initial issue. I am evaluating these argument captors