Test the kotlin project is a new thing. I am glad I have some experience about that and want to introduce it to everyone. Here is what I do.

1. The kotlin project

My project has a main screen, and I have HomeActivity and HomePresenter classes for it. Remember these are two kotlin class, so the file names are : “HomeActivity.kt” and “HomePresenter.kt”.

! [](./_image/2016-12-19 23-08-41.jpg)

Here is some code of HomePresenter.kt:

open class HomePresenter(var view : IHomeView) {
    var repo = getTaskRepository(a)// Injection
    var items = arrayListOf<TaskItem>(a)// a copy data from DataBase

    fun getTaskList(){
        items = repo.getAllTasks()
        view.refreshTaskList()
    }
    .Copy the code

2.1 the setup ()
public class HomePresenterTest {
    private ITaskRepository repo;
    private HomePresenter presenter;
    private IHomeView  view;

    @Before
    public void setUp(){
        view = mock(IHomeView.class);

        repo = new FakeTaskRepository(a); presenter= new HomePresenter(view);
        presenter.setRepo(repo);
    }Copy the code

You see, although IHomeView and HomePresenter is kotlin classes, you can still access them in java class. Because those kotlin class will be converted to “*.class” file eventually. This way, our java classes can access them.

3. another solution

Of course, you can test the kotlin project with Kotlin. All you have to do is just add the kotlin dependency to “test” build variant.

Although I didn’t try this way. But I think the solution below will work. Apparently you’ve already add compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version" to the build.gradle. Now what you have to do is simple, just add this line:

testCompile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"Copy the code