Created
July 4, 2018 15:35
-
-
Save hkhc/b787cf607e177d54253e36c6eb15ba54 to your computer and use it in GitHub Desktop.
Test Activity with Robolectric
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Copyright 2018 Herman Cheung | |
* | |
* Licensed under the Apache License, Version 2.0 (the "License"); | |
* you may not use this file except in compliance with the License. | |
* You may obtain a copy of the License at | |
* | |
* http://www.apache.org/licenses/LICENSE-2.0 | |
* | |
* Unless required by applicable law or agreed to in writing, software | |
* distributed under the License is distributed on an "AS IS" BASIS, | |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
* See the License for the specific language governing permissions and | |
* limitations under the License. | |
* | |
*/ | |
package hkhc.robolectrictest; | |
import android.app.Activity; | |
import android.os.Bundle; | |
import android.support.annotation.Nullable; | |
import android.widget.TextView; | |
import org.junit.Test; | |
import org.junit.runner.RunWith; | |
import org.robolectric.Robolectric; | |
import org.robolectric.RobolectricTestRunner; | |
import org.robolectric.annotation.Config; | |
import static junit.framework.Assert.assertEquals; | |
@RunWith(RobolectricTestRunner.class) | |
@Config(manifest="AndroidManifest.xml", sdk=24) | |
public class SetValueTest { | |
static class Student { | |
private String id; | |
String getId() { | |
return id; | |
} | |
void setId(String id) { | |
this.id = id; | |
} | |
} | |
static class MyActivity extends Activity { | |
TextView tView; | |
Student s; | |
@Override | |
protected void onCreate(@Nullable Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
tView = (TextView) findViewById(R.id.text); | |
someOtherMethod(); | |
} | |
public void setValue() { | |
String id = s.getId(); | |
tView.setText(id); | |
} | |
public void someOtherMethod(){ | |
s =new Student(); | |
s.setId("123"); | |
} | |
} | |
@Test | |
public void testSetValue() { | |
MyActivity myActivity = Robolectric.setupActivity(MyActivity.class); | |
myActivity.setValue();; | |
TextView tView = myActivity.findViewById(R.id.text); | |
assertEquals("123", tView.getText().toString()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment