title: Kotlin equivalent of Swift Expectations/Promises
body: <p>I am trying to write some UnitTests for my native mobile applications, but have ran into a roadblock in my Android Tests. Specifically, I am struggling to find an example of Kotlin's version of Swift's Expectations/Promises.. </p>
  
  <p>I've found examples of Kotlin Promises, but they seem to be way more complicated than needed...</p>
  
  <p>For example, here is a test for the login API function in my iOS project:</p>
  
  <pre><code>func testLogin() {
  
      /// Prepare for login
      if CURRENT_USER != nil {
          logout()
      }
  
      /// Login
      let promise = expectation(description: "User is logged in.")
  
      // 1. Given
      var isSuccess: Bool = false
  
      // 2. When
      User.login(username: maleUsername, password: malePassword, success: {
          isSuccess = true
          promise.fulfill()
      }) { (_, agreeToTerms) in
          XCTFail()
      }
      wait(for: [promise], timeout: maxTimeOut)
  
      // 3. Then
      XCTAssertNotNil(CURRENT_USER)
      XCTAssertTrue(isSuccess)
  
      /// Logout
      logout()
  }
  </code></pre>
  
  <p>This is pretty simple to me. I have an asynchronous method <code>login</code> that has two possible completion blocks: <code>success</code> and <code>failure</code>; and I need to wait for one of them to complete before evaluating. To do this, I create a <strong>promise</strong> before the call, then I <strong>fulfill</strong> the promise in the two completion blocks, and I <strong>wait</strong> for the promise to fulfill before running my assertions.</p>
  
  <p>Now in Kotlin, I have a similar test:</p>
  
  <pre><code>private val loginFragment = LoginFragment()
  
  @Test
  fun loginTest() {
  
      val username = ""
      val password = ""
  
      // TODO: Create Promise
  
      loginFragment.loginViewModel
              .login(username, password)
              .observe(loginFragment, Observer {
                  loginFragment.activity?.onResult(it?.result,
  
                          onSuccess = {
                              // TODO: Fill Promise
                          },
                          onValidationError = {
                              // TODO: Fail Test
                          })
              })
  
      // TODO: Assertions
  
  }
  </code></pre>
  
  <p>But I can't find an equivalent of swift's promises..</p>
  
  <p>Does one exist in Kotlin? If not, how would I implement a version of my Swift's <code>testLogin</code> method in Kotlin?</p>