{"id":16356938,"url":"https://github.com/previousdeveloper/robolectric-gradle-testing","last_synced_at":"2025-12-09T06:30:12.880Z","repository":{"id":148582153,"uuid":"41592314","full_name":"previousdeveloper/Robolectric-Gradle-Testing","owner":"previousdeveloper","description":"How to set up robolectric to work with gradle","archived":false,"fork":false,"pushed_at":"2015-08-29T12:25:59.000Z","size":216,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2024-12-29T06:29:46.020Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"http://www.gokhankaradas.com/android-studio-robolectric-unit-test-yazmak/.html","language":"Java","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/previousdeveloper.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2015-08-29T12:10:30.000Z","updated_at":"2015-08-29T12:24:18.000Z","dependencies_parsed_at":"2023-04-06T11:31:10.095Z","dependency_job_id":null,"html_url":"https://github.com/previousdeveloper/Robolectric-Gradle-Testing","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/previousdeveloper%2FRobolectric-Gradle-Testing","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/previousdeveloper%2FRobolectric-Gradle-Testing/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/previousdeveloper%2FRobolectric-Gradle-Testing/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/previousdeveloper%2FRobolectric-Gradle-Testing/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/previousdeveloper","download_url":"https://codeload.github.com/previousdeveloper/Robolectric-Gradle-Testing/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":239670734,"owners_count":19677866,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-10-11T01:44:35.283Z","updated_at":"2025-12-09T06:30:12.829Z","avatar_url":"https://github.com/previousdeveloper.png","language":"Java","readme":"# Robolectric-Gradle-Testing\n\n1. [English](#)\n\nRobolectric, unit testleri oluşturmak için geliştirilmiş bir frameworktur.Robolectric testleri JVM üzerinde koşar, herhangi bir cihaz veya emülatör üzerinde çalışmaz.\n\nJVM üzerinde çalıştığı için CI(continuous integration) için bize çok büyük kolaylık sağlar.Herhangi ek bir konfigürasyon yapmadan unit testlerimizi Jenkins gibi CI tool yardımıyla ile çalıştırabiliriz.\n\nRobolectric bize view üzerinde elementlere,resource kaynaklarına erişmeyi sağlamıştır.\n\n## Robolectric kurulumu ve konfigürasyon ayarları\n\nAndroid studio oluşturduğumuz başlangıç projesinde dosya hiyerarşisinde **androidTest** ve **main** dosyaları gelmektedir.Robolectric ile unit testleri çalıştırabilmemiz için **test/java** adında bir dosya oluşturmak gerekmektedir.\n\n![Build Variants](http://www.gokhankaradas.com/wp-content/uploads/2015/08/studio.jpg)\n\nArdından Android Studio solunda bulunan Build Variants kısmından Test Artifactini **Unit Test** olarak seçelim. Yukarıda görebilirsiniz.\n\n## Gradle Plugin Gerekli Bağımlılıkların Yüklenmesi\n\n```java\ndependencies {\n    compile fileTree(dir: 'libs', include: ['*.jar'])\n    compile 'com.android.support:appcompat-v7:22.2.0'\n    // Junit ve roboelectric tanımlanması\n    //Test Framework\n    testCompile 'junit:junit:4.12'\n    testCompile \"org.robolectric:robolectric:3.0\"\n}\n```\n\nÖrnek bir test yazımı ve çalıştırılması.\n\nActivity içinde yaptığımız basit işlemler.\n```java\npublic class MainActivity extends Activity {\n\n    private Button mBtn;\n    private TextView mTextView;\n    private ListView mListView;\n    private List\u003cString\u003e stringList;\n    private ArrayAdapter\u003cString\u003e listAdapter;\n\n    @Override\n    protected void onCreate(Bundle savedInstanceState) {\n        super.onCreate(savedInstanceState);\n        setContentView(R.layout.activity_main);\n        mBtn = (Button) findViewById(R.id.test_button);\n        mTextView = (TextView) findViewById(R.id.textView);\n        mListView = (ListView) findViewById(R.id.root_listview);\n\n        mBtn.setOnClickListener(new View.OnClickListener() {\n            @Override\n            public void onClick(View v) {\n\n                mTextView.setText(\"changed\");\n            }\n        });\n        adapter();\n    }\n\n    private void adapter() {\n\n        stringList = new ArrayList\u003c\u003e();\n        for (int i = 1; i \u003c 21; i++) {\n            stringList.add(String.valueOf(i));\n        }\n\n        listAdapter = new ArrayAdapter\u003cString\u003e(this, android.R.layout.simple_list_item_1, stringList);\n        mListView.setAdapter(listAdapter);\n    }\n```\n\nLayout tarafında oluşturduğumuz bileşenler\n```java\n\u003cRelativeLayout xmlns:android=\"http://schemas.android.com/apk/res/android\"\n    xmlns:tools=\"http://schemas.android.com/tools\" android:layout_width=\"match_parent\"\n    android:layout_height=\"match_parent\" android:paddingLeft=\"@dimen/activity_horizontal_margin\"\n    android:paddingRight=\"@dimen/activity_horizontal_margin\"\n    android:paddingTop=\"@dimen/activity_vertical_margin\"\n    android:paddingBottom=\"@dimen/activity_vertical_margin\" tools:context=\".MainActivity\"\n    android:id=\"@+id/root_layout\"\u003e\n\n    \u003cTextView android:text=\"@string/hello_world\" android:layout_width=\"wrap_content\"\n        android:layout_height=\"wrap_content\"\n        android:id=\"@+id/textView\" /\u003e\n\n    \u003cButton\n        android:layout_width=\"wrap_content\"\n        android:layout_height=\"wrap_content\"\n        android:text=\"New Button\"\n        android:id=\"@+id/test_button\"\n        android:layout_below=\"@+id/textView\"\n        android:layout_alignParentLeft=\"true\"\n        android:layout_alignParentStart=\"true\" /\u003e\n\n    \u003cListView\n        android:layout_width=\"wrap_content\"\n        android:layout_height=\"wrap_content\"\n        android:id=\"@+id/root_listview\"\n        android:layout_alignParentBottom=\"true\"\n        android:layout_toRightOf=\"@+id/test_button\"\n        android:layout_toEndOf=\"@+id/test_button\" /\u003e\n\n\u003c/RelativeLayout\u003e\n```\n\n### Test Kodlarının yazılması\n```java\n@RunWith(RobolectricGradleTestRunner.class)\n@Config(constants = BuildConfig.class)\npublic class SampleTest {\n\n    private Button test_button;\n    private TextView textView;\n    private ListView mListView;\n    private MainActivity activity;\n\n    @Before\n    public void setUp() throws Exception {\n        activity = Robolectric.setupActivity(MainActivity.class);\n        test_button = (Button) activity.findViewById(R.id.test_button);\n        textView = (TextView) activity.findViewById(R.id.textView);\n        mListView = (ListView) activity.findViewById(R.id.root_listview);\n\n    }\n\n    @Test\n    public void shouldMainActivityNotBeNull() throws Exception {\n\n        assertTrue(Robolectric.buildActivity(MainActivity.class).create().get() != null);\n\n        String hello = new MainActivity().getResources().getString(R.string.hello_world);\n\n        Assert.assertEquals(hello, \"Hello world!\");\n    }\n\n    @Test\n    public void shouldTextViewNameChanged() throws Exception {\n\n        test_button.performClick();\n        Assert.assertEquals(textView.getText().toString(), \"changed\");\n    }\n\n    @Test\n    public void shouldRootLayoutLoaded() {\n\n        Assert.assertEquals(R.id.root_layout, shadowOf(activity).getContentView().getId());\n    }\n\n\n    @Test\n    public void shouldApplicationIdBeCorrect() {\n        String appId = BuildConfig.APPLICATION_ID;\n        Assert.assertEquals(appId, \"testcase.unittestwithroboelectric\");\n    }\n\n    @Test\n    public void shouldListViewItemSizeGreaterThanO() {\n        int listViewItemSize = mListView.getCount();\n        Assert.assertTrue(listViewItemSize \u003e 0);\n    }\n\n    @Test\n    public void shouldButtonNameCorrect(){\n        Assert.assertEquals(test_button.getText().toString(),\"New Button\");\n    }\n}\n```\n\n![Sonuclarin Görüntülenmesi](http://www.gokhankaradas.com/wp-content/uploads/2015/08/test_results-1024x317.jpg)\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpreviousdeveloper%2Frobolectric-gradle-testing","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fpreviousdeveloper%2Frobolectric-gradle-testing","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpreviousdeveloper%2Frobolectric-gradle-testing/lists"}