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.
- While in the Shape.java I go to "Navigate" > "Test"
- Hit enter to select "Create new Test"
- Get this popup, and select JUNIT4.
- I then hit the fix button to fix the library not being found
- I get this popup
- I'm not really sure what to select, so I select the default/highlighted.
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);
}
}
At this point, I'm not really sure how to run my tests, but try to do this:
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
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…