I've been struggling for a while with this. When creating a test suite in JUnit/Android, I can to the following:
Add all the tests (in all the classes) that exist in the same package as the suite
Add a specific class contatining testMethods
However, I'm completely unable to do the following:
Add a specific testMethod from a specific class to the test suite.
Now, I understand that this SHOULD be possible, as there are countless examples showing this.
This is how it's supposed to work:
The test class contatining the test methods:
import com.frank.android.lookup.SomeClass;
import android.test.AndroidTestCase;
public class ArithmeticsTests extends AndroidTestCase {
SomeClass sctest;
protected void setUp () throws Exception {
sctest = new SomeClass();
super.setUp();
}
public void testAddNumbers () {
assertEquals(9, sctest.addNumbers(3, 6));
}
public void testSubtractNumbers () {
assertEquals(2, sctest.subtractNumbers(6, 4));
}
protected void tearDown () throws Exception {
super.tearDown();
}
}
And here's the test suite class:
import junit.framework.TestSuite;
public class ProjectTestSuite_SomeTests extends TestSuite {
public static Test suite () {
TestSuite suite = new TestSuite("ArithmeticsTests");
suite.addTest(new ArithmeticsTests("testAddNumbers"));
suite.addTest(new ArithmeticsTests2("testSubtractNumbers"));
return suite;
}
}
Now, the two lines where I try adding the individual test methods result in this error:
The constructor ArithmeticsTests(String) is undefined
Now, I've looke around for a long time, and I cannot find any explanation for this. It seems that something is missing, since it doesn't understand what I'm trying to do. The "string" it complains about is in fact the name of the method - I'm not trying to pass a string to a constructor of the class - I'm trying to add the method of the class to the test suite.
I'm using the JUnit version that's included with the Android SDK here, and I haven't installed anything else related to that. Is there something missing? (Obviously there is, bit what?)
EDIT:
I added a construtor to the ArithmeticsTests class:
public ArithmeticsTests (String s) {}
Now the above error is gone.
However, when I run the test suite, I get this error:
testSuiteCreationFailed
....
Caused by: java.lang.NullPointerException: Method name must not be null.
I came up with the same problem and discovered that, while AndroidTestCase does not have a constructor taking a String parameter, it does have a setName(String name) method. By calling the setName method, you can add individual method to the test case.
Using the code in your example, your test suite may look like:
import junit.framework.TestSuite;
public class ProjectTestSuite_SomeTests extends TestSuite {
public static Test suite () {
TestSuite suite = new TestSuite("ArithmeticsTests");
ArithmeticsTests arithmeticsTests = new ArithmeticsTests();
arithmeticsTests.setName("testAddNumbers");
suite.addTest(arithmeticsTests);
ArithmeticsTests2 arithmeticsTest2 = new ArithmeticsTests2();
arithmeticsTest2.setName("testSubtractNumbers");
suite.addTest(arithmeticsTest2);
return suite;
}
}
This worked for me.
public static TestSuite suite() {
final TestSuite t = new TestSuite();
t.addTest(TestSuite.createTest(TestExampleClass1.class, "test1"));
t.addTest(TestSuite.createTest(TestExampleClass2.class, "test2"));
t.addTest(TestSuite.createTest(TestExampleClass3.class, "test2"));
return t;
}
Related
I'm pretty new to Espresso, but I am trying to test a relatively simple Activity. My android app has its own custom Application class. How can I tell Espresso to use a mocked (or custom) version of this class?
Here is my custom version of the Application. It creates some test data (edited here for brevity). Down the road, I will also be overriding some of the methods.
public class MockMyApplication extends MyApplication {
#Override
public void onCreate() {
super.onCreate();
// create some location data for testing
DataRecord rec = new DataRecord(1);
rec.setName("TestLoc1");
rec.setDescription("an important customer");
MyData.add(rec);
}
}
My attempt to test using this, looks like this:
#RunWith(AndroidJUnit4.class)
#LargeTest
public class LocEditActivityTest extends AndroidJUnitRunner {
#Rule
public ActivityTestRule<LocEditActivity> activityTestRule
= new ActivityTestRule<>(LocEditActivity.class);
#Override
public Application newApplication(ClassLoader cl, String className, Context context) throws IllegalAccessException, ClassNotFoundException, InstantiationException {
return super.newApplication(cl, MockMyApplication.class.getName(), context);
}
#Test
public void testActivity_ExistingLoc() {
Intent i = new Intent();
i.putExtra("loc",1);
activityTestRule.launchActivity(i);
onView(withId(R.id.editName)).check(matches(withText("TestLoc1")));
// shutdown
onView(withContentDescription("Navigate up")).perform(click());
}
}
Using a debugger, I have determined that when LocEditAcitivity's onCreate calls getApplication(), it returns a MyApplication class with empty data, and not the MockedMyApplication with my test data.
Found it!
Looks like I misunderstood the "Runner" class usage. I needed to create my own Runner that extended AndroidJUnitRunner:
import android.app.Application;
import android.content.Context;
import android.support.test.runner.AndroidJUnitRunner;
// Our own test runniner - uses MockMyApplication as a mocked app class
public class MyAndroidTestRunner extends AndroidJUnitRunner {
#Override
public Application newApplication(ClassLoader cl, String className, Context context) throws IllegalAccessException, ClassNotFoundException, InstantiationException {
return super.newApplication(cl, MockMyApplication.class.getName(), context);
}
}
And then in build.gradle (app), the testInstrumentationRunner entry needs to point to the new runner:
testInstrumentationRunner "com.winwaed.xyzapp.MyAndroidTestRunner"
As the newApplication override was in the wrong place, this should be removed from my test class. Also, the test class no longer extends any classes. (ie. I essentially split the runner and test classes - as I said, I misunderstood the runner class)
How to verify a void method call in Robolectric test case where as no data coming out the called method.
What to assert in this case? Below given an example of the requirement.
public class SampleClass(){
final String TAG = SampleClass.class.getSimpleName();
public void log(){
Log.d(TAG, "Entry Loggd");
}
}
#Test
public void logEntry_test(){
SampleClass sc = new SampleClass();
sc.log();
// What to assert here to verify this log method
}
First off, good on you for writing tests!!! There are a few ways to go about testing that an internal logger is called. It's equally as important to understand what you're looking to test. Testing that a class is logging a specific message is most likely a fragile test, so be fore-warned that you probably don't need it.
Method #1: Using Robolectric
Robolectic documentation doesn't lend itself to answering basic questions, but its codebase is very well documented with its tests. A basic understanding of its principles and how shadows work can get you a long way. ShadowLog tests lay the ground work to this solution.
#RunWith(RobolectricTestRunner.class)
public class SampleClassTest {
#Test
public void log_writesExpectedMessage() {
new SampleClass().log();
ShadowLog.LogItem lastLog = ShadowLog.getLogs().get(0);
assertThat(lastLog.msg).isEqualTo("some message");
// or
assertThat(lastLog.msg).isNotNull();
}
}
Tests using Robolectric v3.1.2
Add the following to your build.gradle file:
testCompile 'org.robolectric:robolectric:3.1.2'
Method #2: Making use of Abstractions
If your sample class derives from an Android class (Activity, Fragment, Application, etc), then using android.util.Log makes sense, but bear in mind that your test will need to be a Robolectric or AndroidInstrumented test. If your SampleClass is just some POJO, then using a simple logging framework may make your testing efforts easier. For example, using Jake Wharton's Timber, your class and test can be written as follows:
import timber.log.Timber;
public class SampleClass {
void log() {
Timber.d("some message");
}
}
// SampleClassTest.java
public class SampleClassTest {
// setting up a Tree instance that we define below
TestTree testTree = new TestTree();
#Test
public void log_writesExpectedMessage() {
// setting up Timber to us the test classes log writer
Timber.plant(testTree);
// invoke the logging function
new SampleClass().log();
// assert
assertThat(testTree.lastMessage).isEqualTo("some message");
}
private class TestTree extends Timber.Tree {
private String lastMessage;
#Override
protected void log(int priority, String tag, String message, Throwable t) {
lastMessage = message;
}
}
}
Good luck, happy testing!
In my understanding you want to mock static methods. I guess, using static mocks are not the most elegant way to testing. Better to use an abstraction as recommended by abest. Although, it can be done with PowerMock.
I'm trying out Android development, but haven't come too far because I'm unable to get a test case to fail.
I have the following test case in the androidTest folder:
package com.example.aaronf.myapplication;
import android.test.*;
public class ToDoListTest extends AndroidTestCase {
private void newToDoListHasNoItems() {
assertEquals(new ToDoList().length, 0);
}
private void addingToDoGivesLengthOfOne() {
ToDoList toDoList = new ToDoList();
toDoList.add(new ToDo());
assertEquals(toDoList.length, 1);
}
public void runTests() {
newToDoListHasNoItems();
addingToDoGivesLengthOfOne();
}
public ToDoListTest() {
super();
runTests();
}
}
The ToDoList class looks like:
package com.example.aaronf.myapplication;
public class ToDoList {
public int length = 0;
public void add(ToDo toDo) {
}
}
It seems like it should fail on addingToDoGivesLengthOfOne(), but I get a green bar.
EDIT
I should add that adding #Test annotations to the methods generates a symbol not found error.
EDIT
I visited the suggested post My Junit test doesn't run. However, there is a difference with my problem. My methods used to have the test prefix, but this didn't affect the outcome. Also, the #Test annotation, as I mentioned before, is flagged with an error: "Cannot resolve symbol Test".
The problem was that my Test Artifact was set to Android Instrumentation Tests instead of Unit Tests. Since my unit tests were being added to the Android Instrumentation group, the unit testing stuff wasn't being recognized.
I have problem with creating JUnit Test Automation.
My project has many activities (some activities inside other activities).
So I create testcase for each activity. But the problem how can i call a testcase inside other testcases (like activity inside other activities).
Can any one give me some idear?
Thanks.....
You tests should live in a different project not with your Activities.
Then the test runner, usually InstrumentationTestRunner, will be able to discover and run your test cases using instrospection.
Disclaimer: this can get very, very messy. If you need one test case to spawn another test case, there's probably a better way of doing it.
JUnit operates on classes. If you want to create tests at runtime, you have to create classes at runtime. Here, the method specializedTester creates an anonymous subclass where getInstance() returns specialized Activity objects for testing.
public abstract class ActivityTestCase extends TestCase {
public abstract Activity getInstance();
public static Class specializedTester(final String specialty) {
return new ActivityTestCase() {
public Activity getInstance() {
return new Activity(specialty);
}
};
}
public void testChildActivities() {
Activity activity = getInstance();
for(Activity a : activity.children()) {
// "check ripeness", "bargain hunt", "check out", etc
Class c = specializedTester(a.specialty);
suite.addTestSuite(c);
}
}
static TestSuite suite;
public static void main(String[] args) {
suite = new TestSuite(ActivityTestCase.specializedTester("buy groceries"));
TestRunner.run(suite);
}
}
I've got the basic scenario: a test project, in it - one test class, nothing less, nothing more. The code of the test class is this:
public class SManagerTest extends AndroidTestCase {
private SManager sm;
public SManagerTest(){
sm = SManager.getInstance(getContext());
}
#Test
public void trainTest(){
sm.go();
}
}
What's wrong? because I get this:
Test run failed: Test run incomplete. Expected 1 tests, received 0
Thanks!
As pointed out earlier use "test" with a small t and don't forget to run it as an "Android JUnit Test"
Agree with Christopher - start the method name with 'test' even though you're adding the #Test decorator. Also, add this class in the same folder as your tests and it will run all your tests:
public class AllTests extends TestSuite
{
public static Test suite()
{
return new TestSuiteBuilder(AllTests.class).includeAllPackagesUnderHere().build();
}
}