Question: I am creating a **iOS library** with a class that extends UIView and I need to perform unit testing on it.
But this custom class need to be inserted in a ViewController and visible to the user to execute correctly. It does also check if the view.window != nil.

In a **iOS application** unit test method, I've inserted a UIViewController using:

```
UIViewController *testViewController = [[MyViewController alloc] init];
UIApplication.sharedApplication.keyWindow.rootViewController = testViewController;

```

With that I could insert my custom view in the view controller and perform unit tests in it.

But in the **iOS library** project unit test method the UIApplication.sharedApplication returns nil.

Is there an alternative way to perform UI test in a iOS library?

Answer: 
Answering my own question.

In my specific case it was crucial to the view.window != nil.

UIApplication.sharedApplication.keyWindow really is nil in library unit tests.

Instead of it, I've just used:

```
UIWindow *window = [[UIWindow alloc] init];
window.rootViewController = testViewController;

```

Simple as that.
If you would like to have all "view life cycle" methods called on a viewController then try this:

```
let viewController = UIViewController()
let window = UIWindow()
let window.addSubview(viewController.view)

```