mardi 3 mars 2015

How to run a simple JUnit4 test in Android Studio 1.1?

I have an Android project that shows "Hello World". It was created from the "Blank Activity" template from Android Studio.


I then add/create a new java class in my application package (the same package that has my activity). I call it Shape and add a simple constructor



public class Shape {
public Shape(int i){
if (i==0){
throw new IllegalArgumentException("Cant have 0");
}
}
}


Great. Now I have a class that isn't touching Android at all, and I want to unit test it. What should I do next?


This is where my question stops. Below I'll go through what I tried.


Please note that I really have never tested before in Android or Java. Excuse me for "rookie" mistakes.



  1. While in the Shape.java I go to "Navigate" > "Test"

  2. Hit enter to select "Create new Test"

  3. Get this popup, and select JUNIT4. enter image description here

  4. I then hit the fix button to fix the library not being found

  5. I get this popup enter image description here

  6. I'm not really sure what to select, so I select the default/highlighted.


  7. I write my test


    package com.eghdk.getjunit4towork;


    import org.junit.Test;


    import static org.junit.Assert.*;


    public class ShapeTest { @Test(expected = IllegalArgumentException.class) public void testShapeWithInvalidArg() { new Shape(0); } }




  8. At this point, I'm not really sure how to run my tests, but try to do this: enter image description here




  9. I get these errors when running


    Error:(3, 17) Gradle: error: package org.junit does not exist Error:(5, 24) Gradle: error: package org.junit does not exist Error:(8, 6) Gradle: error: cannot find symbol class Test




Aucun commentaire:

Enregistrer un commentaire