title: Mocking URLSessionDataTask without using init
body: <p>I'm trying to mock my <code>URLSession</code> instance and after researching on StackOverflow, I've seen that one approach is to create a mocked URLSessionDataTask and call the completion handler on <code>resume()</code>:</p>
  <pre><code>class URLSessionDataTaskMock: URLSessionDataTask {
      private let closure: () -&gt; Void
  
      init(closure: @escaping () -&gt; Void) {
          self.closure = closure
      }
  
      override func resume() {
          closure()
      }
  
      override func cancel() {
          closure()
      }
  }
  
  class URLSessionMock: URLSession {
      typealias CompletionHandler = (Data?, URLResponse?, Error?) -&gt; Void
      var data: Data?
      var error: Error?
      override func dataTask(
          with _: URLRequest,
          completionHandler: @escaping CompletionHandler
      ) -&gt; URLSessionDataTask {
          let data = self.data
          let error = self.error
          return URLSessionDataTaskMock {
              completionHandler(data, nil, error)
          }
      }
  }
  </code></pre>
  <p>The problem I'm facing is that <code>URLSessionDataTask.init</code> has been deprecated as of iOS 13:</p>
  <blockquote>
  <p>'init()' was deprecated in iOS 13.0: Please use -[NSURLSession dataTaskWithRequest:] or other NSURLSession methods to create instances</p>
  </blockquote>
  <p>Is there an updated approach to achieve the same goal?</p>