0

I am writing Unit test for usecase in kotlin but don't know how to cover missing branch.

// ConnectFTPUseCaseImpl

// ConnectFTPUseCase
interface ConnectFTPUseCase {
    suspend fun connect(): Result<Unit>
}

class ConnectFTPUseCaseImpl @Inject constructor(
    @IoDispatcher dispatcher: CoroutineDispatcher,
    private val ftpRepository: FTPRepository,
): UseCase<Unit, Unit>(dispatcher), ConnectFTPUseCase {

    override suspend fun execute(parameters: Unit) {
        return ftpRepository.connectFTP()
    }

    override suspend fun connect(): Result<Unit> = invoke(Unit)
}
abstract class UseCase<in P, R>(private val coroutineDispatcher: CoroutineDispatcher) {

    suspend operator fun invoke(parameters: P): Result<R> {
        return runCatching {
            execute(parameters)
        }
    }

    @Throws(RuntimeException::class)
    protected abstract suspend fun execute(parameters: P): R
}

And I'm missing a branch like this. evd

I can cover 100% by calling execute directly like this:

class ConnectFTPUseCaseImpl @Inject constructor(
    @IoDispatcher dispatcher: CoroutineDispatcher,
    private val ftpRepository: FTPRepository,
): UseCase<Unit, Unit>(dispatcher), ConnectFTPUseCase {

    override suspend fun execute(parameters: Unit) {
        return ftpRepository.connectFTP()
    }

    override suspend fun connect(): Result<Unit> = runCatching {
        execute(Unit)
    }
}

I don't know what the cause is. Is there any way to cover this case without updating the code?

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.