Consider such code: ```data class Task(val name: S...
# spring
i
Consider such code:
Copy code
data 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:
Copy code
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?
Copy code
@GenerateFactory
data class Task(val name: String, val description: String)
s
If
TaskFactory
is a functional interface, you should be able pass
::Task
as the instance. Does that help?
i
Can you elaborate a bit more on this?
s
I might be wrong, actually. I was hoping that this would work:
Copy code
fun 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 references
Oh wait, my syntax was wrong
Copy code
fun interface TaskFactory {
    fun create(name: String, description: String): Task
}

data class Task(val name: String, val description: String)

val factory = TaskFactory(::Task)
i
I was thinking about more complete solution that will generate whole factory class/interface code
k
why do you need factory?
i
I need factory to test that repo is called with the task (
repository.save(task)
) eg. that
name
String was not accidentally switched with
description
string. How would you test this?
k
well still, how would factory help this? you just wrapped constructor. you can test constructor separately if that's what you want. in this case, you have request model as input and save method call sideeffect as output. so I would use some argumentcaptor equivalent and verify that method was call via mock/spy
i
Nice idea @kqr I will give this a try thx
đź‘Ť 1
t
Would something like this do the trick? Depending on how complex the TaskRequestModel is, you might not need to mock that either.
i
CreateTaskUseCase
serves as a factory, so this basically is the same as initial issue. I am evaluating these
argument captors