From 20fc7c51eb6e4ea80f4a2e55457524c24a844a04 Mon Sep 17 00:00:00 2001 From: Alex Baker Date: Thu, 24 Oct 2013 22:41:13 -0500 Subject: [PATCH] Convert DI, SQL tests to robolectric --- .../service/DependencyInjectionTest.java} | 17 +++++++++++-- .../andlib/sql/QueryTemplateHelperTest.java | 24 ++++++++++++++----- .../AstridDependencyInjectorTest.java} | 20 ++++++++++------ 3 files changed, 46 insertions(+), 15 deletions(-) rename astrid/src/{instrumentTest/java/com/todoroo/andlib/service/DependencyInjectionTests.java => test/java/com/todoroo/andlib/service/DependencyInjectionTest.java} (94%) rename astrid/src/{instrumentTest => test}/java/com/todoroo/andlib/sql/QueryTemplateHelperTest.java (92%) rename astrid/src/{instrumentTest/java/com/todoroo/astrid/service/AstridDependencyInjectorTests.java => test/java/com/todoroo/astrid/service/AstridDependencyInjectorTest.java} (84%) diff --git a/astrid/src/instrumentTest/java/com/todoroo/andlib/service/DependencyInjectionTests.java b/astrid/src/test/java/com/todoroo/andlib/service/DependencyInjectionTest.java similarity index 94% rename from astrid/src/instrumentTest/java/com/todoroo/andlib/service/DependencyInjectionTests.java rename to astrid/src/test/java/com/todoroo/andlib/service/DependencyInjectionTest.java index 097975906..0228f35c6 100644 --- a/astrid/src/instrumentTest/java/com/todoroo/andlib/service/DependencyInjectionTests.java +++ b/astrid/src/test/java/com/todoroo/andlib/service/DependencyInjectionTest.java @@ -5,12 +5,20 @@ */ package com.todoroo.andlib.service; -import android.test.AndroidTestCase; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.robolectric.RobolectricTestRunner; import java.lang.reflect.Field; -public class DependencyInjectionTests extends AndroidTestCase { +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.fail; +@RunWith(RobolectricTestRunner.class) +public class DependencyInjectionTest { + + @Test public void testNoAutowire() { DependencyInjectionService service = new DependencyInjectionService(); @@ -18,6 +26,7 @@ public class DependencyInjectionTests extends AndroidTestCase { service.inject(test); } + @Test public void testSimpleStringInjectionAutowire() { DependencyInjectionService service = new DependencyInjectionService(); service.addInjector( @@ -94,6 +103,7 @@ public class DependencyInjectionTests extends AndroidTestCase { assertNull( test.toString()); } + @Test public void testHierarchicalStringInjectionAutowire() { DependencyInjectionService service = new DependencyInjectionService(); service.addInjector( @@ -141,6 +151,7 @@ public class DependencyInjectionTests extends AndroidTestCase { assertEquals("malarkey", test.toString()); } + @Test public void testMissingInjection() { DependencyInjectionService service = new DependencyInjectionService(); service.addInjector( @@ -175,6 +186,7 @@ public class DependencyInjectionTests extends AndroidTestCase { assertNull(test.toString()); } + @Test public void testMultipleInjection() { DependencyInjectionService service = new DependencyInjectionService(); service.addInjector( @@ -223,6 +235,7 @@ public class DependencyInjectionTests extends AndroidTestCase { protected String bar; } + @Test public void testInheritedInjection() { DependencyInjectionService service = new DependencyInjectionService(); service.addInjector( diff --git a/astrid/src/instrumentTest/java/com/todoroo/andlib/sql/QueryTemplateHelperTest.java b/astrid/src/test/java/com/todoroo/andlib/sql/QueryTemplateHelperTest.java similarity index 92% rename from astrid/src/instrumentTest/java/com/todoroo/andlib/sql/QueryTemplateHelperTest.java rename to astrid/src/test/java/com/todoroo/andlib/sql/QueryTemplateHelperTest.java index 152a07d4e..e9723230b 100644 --- a/astrid/src/instrumentTest/java/com/todoroo/andlib/sql/QueryTemplateHelperTest.java +++ b/astrid/src/test/java/com/todoroo/andlib/sql/QueryTemplateHelperTest.java @@ -5,18 +5,24 @@ */ package com.todoroo.andlib.sql; -import android.test.AndroidTestCase; - import com.todoroo.andlib.sql.Query.QueryTemplateHelper; import com.todoroo.astrid.dao.TaskDao.TaskCriteria; import com.todoroo.astrid.data.Task; -public class QueryTemplateHelperTest extends AndroidTestCase { +import org.junit.Test; +import org.junit.runner.RunWith; +import org.robolectric.RobolectricTestRunner; + +import static org.junit.Assert.assertEquals; + +@RunWith(RobolectricTestRunner.class) +public class QueryTemplateHelperTest { public StringBuilder selection = new StringBuilder(); public StringBuilder order = new StringBuilder(); public StringBuilder groupBy = new StringBuilder(); + @Test public void testBasic() { QueryTemplateHelper.queryForContentResolver("", selection, order, groupBy); @@ -25,6 +31,7 @@ public class QueryTemplateHelperTest extends AndroidTestCase { assertEquals(groupBy.toString(), ""); } + @Test public void testSelection() { QueryTemplateHelper.queryForContentResolver("WHERE foo = bar", selection, order, groupBy); @@ -33,6 +40,7 @@ public class QueryTemplateHelperTest extends AndroidTestCase { assertEquals(groupBy.toString(), ""); } + @Test public void testOrder() { QueryTemplateHelper.queryForContentResolver("ORDER BY cats", selection, order, groupBy); @@ -41,6 +49,7 @@ public class QueryTemplateHelperTest extends AndroidTestCase { assertEquals(groupBy.toString(), ""); } + @Test public void testWhereOrder() { QueryTemplateHelper.queryForContentResolver("WHERE foo = bar ORDER BY cats", selection, order, groupBy); @@ -49,6 +58,7 @@ public class QueryTemplateHelperTest extends AndroidTestCase { assertEquals(groupBy.toString(), ""); } + @Test public void testGroupBy() { QueryTemplateHelper.queryForContentResolver("GROUP BY dogs", selection, order, groupBy); @@ -57,6 +67,7 @@ public class QueryTemplateHelperTest extends AndroidTestCase { assertEquals(groupBy.toString(), "dogs"); } + @Test public void testWhereGroupBy() { QueryTemplateHelper.queryForContentResolver("WHERE foo = bar GROUP BY dogs", selection, order, groupBy); @@ -65,6 +76,7 @@ public class QueryTemplateHelperTest extends AndroidTestCase { assertEquals(groupBy.toString(), "dogs"); } + @Test public void testOrderGroupBy() { QueryTemplateHelper.queryForContentResolver("GROUP BY dogs ORDER BY cats", selection, order, groupBy); @@ -72,6 +84,7 @@ public class QueryTemplateHelperTest extends AndroidTestCase { assertEquals(groupBy.toString(), "dogs"); } + @Test public void testWhereGroupByAndOrder() { QueryTemplateHelper.queryForContentResolver("WHERE foo = bar GROUP BY dogs ORDER BY cats", selection, order, groupBy); @@ -80,6 +93,7 @@ public class QueryTemplateHelperTest extends AndroidTestCase { assertEquals(groupBy.toString(), "dogs"); } + @Test public void testRealQueryTemplate() { QueryTemplateHelper.queryForContentResolver( new QueryTemplate().where(TaskCriteria.completed()). @@ -90,6 +104,7 @@ public class QueryTemplateHelperTest extends AndroidTestCase { assertEquals("", groupBy.toString()); } + @Test public void testRealQueryTemplateTwo() { QueryTemplateHelper.queryForContentResolver( new QueryTemplate().where(TaskCriteria.isActive()). @@ -99,7 +114,4 @@ public class QueryTemplateHelperTest extends AndroidTestCase { assertEquals(Order.asc(Task.ELAPSED_SECONDS).toString(), order.toString()); assertEquals(Task.NOTES.toString(), groupBy.toString()); } - - - } diff --git a/astrid/src/instrumentTest/java/com/todoroo/astrid/service/AstridDependencyInjectorTests.java b/astrid/src/test/java/com/todoroo/astrid/service/AstridDependencyInjectorTest.java similarity index 84% rename from astrid/src/instrumentTest/java/com/todoroo/astrid/service/AstridDependencyInjectorTests.java rename to astrid/src/test/java/com/todoroo/astrid/service/AstridDependencyInjectorTest.java index 508299a16..a0b5305c6 100644 --- a/astrid/src/instrumentTest/java/com/todoroo/astrid/service/AstridDependencyInjectorTests.java +++ b/astrid/src/test/java/com/todoroo/astrid/service/AstridDependencyInjectorTest.java @@ -5,12 +5,18 @@ */ package com.todoroo.astrid.service; -import android.test.AndroidTestCase; - import com.todoroo.andlib.service.Autowired; import com.todoroo.andlib.service.DependencyInjectionService; -public class AstridDependencyInjectorTests extends AndroidTestCase { +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.robolectric.RobolectricTestRunner; + +import static org.junit.Assert.assertTrue; + +@RunWith(RobolectricTestRunner.class) +public class AstridDependencyInjectorTest { protected static class Helper { public Object getObject() { @@ -18,16 +24,15 @@ public class AstridDependencyInjectorTests extends AndroidTestCase { } } - @Override - protected void setUp() throws Exception { - super.setUp(); - + @Before + public void setUp() throws Exception { // in case some state from other unit tests overwrote injector DependencyInjectionService.getInstance().addInjector( new AstridDependencyInjector() ); } + @Test public void testWithString() { Helper helper = new Helper() { @Autowired @@ -43,6 +48,7 @@ public class AstridDependencyInjectorTests extends AndroidTestCase { assertTrue(((String)helper.getObject()).length() > 0); } + @Test public void testWithClass() { Helper helper = new Helper() {