Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update example to be consistent with robolectric.org #7475

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
18 changes: 9 additions & 9 deletions README.md
Expand Up @@ -12,18 +12,18 @@ Robolectric supports running unit tests for *16* different versions of Android,
Here's an example of a simple test written using Robolectric:

```java
@RunWith(AndroidJUnit4.class)
@RunWith(RobolectricTestRunner.class)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@seadowg What about keeping using AndroidJUnit4?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah I'd also recommend using AndroidJUnit4 by default as well.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I went with RobolectricTestRunner to keep it consistent with http://robolectric.org/. I had switched away from using AndroidJUnit4 on the website because using that runner requires also having androidx.test.ext:junit as a dependency.

Personally, I like the idea of having Robolectric docs focus on Robolectric in isolation and then have extra docs that describe using it as part of AndroidX Test (like http://robolectric.org/androidx_test/ and Android official docs).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@hoisie I agree with @seadowg thought. Maybe we can add new commits to add special examples with AndroidJUnit4.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any update on the thinking here?

Copy link
Collaborator Author

@seadowg seadowg Nov 7, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But @hoisie has different opinion about switching AndroidJUnit4 to RobolectricTestRunner.

Is this coming from a discussion outside this thread? @hoisie hasn't replied to my initial comment.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah I'd also recommend using AndroidJUnit4 by default as well.

@seadowg This one.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry for the delay in replying @utzcoz! I'm happy to wait for a reply from @hoisie to my follow-up on that if we're not happy to merge on the single approval:

I went with RobolectricTestRunner to keep it consistent with http://robolectric.org/. I had switched away from using AndroidJUnit4 on the website because using that runner requires also having androidx.test.ext:junit as a dependency.

I think we'll end up repeating these points in a new PR otherwise, and I don't think the change to try-with-resources is high priority enough to need to get through now seeing as we've already made the change to the website.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any update on this? If it's still preferred that we use AndroidJUnit4 as the runner in examples, then I'd suggest we also add androidx.test.ext:junit as a dependency in the "Starting a New Project" section. Happy to go with that if it'd make more sense to everyone!

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@seadowg Sorry for delaying response. Current state looks great to me. But it needs @hoisie review suggestions.

public class MyActivityTest {

@Test
public void clickingButton_shouldChangeResultsViewText() {
Activity activity = Robolectric.setupActivity(MyActivity.class);

Button button = (Button) activity.findViewById(R.id.press_me_button);
TextView results = (TextView) activity.findViewById(R.id.results_text_view);

button.performClick();
assertThat(results.getText().toString(), equalTo("Testing Android Rocks!"));
public void clickingButton_shouldChangeMessage() {
try (ActivityController<MyActvitiy> controller = Robolectric.buildActivity(MyActvitiy.class)) {
controller.setup(); // Moves Activity to RESUMED state
MyActvitiy activity = controller.get();

activity.findViewById(R.id.button).performClick();
assertEquals(((TextView) activity.findViewById(R.id.text)).getText(), "Robolectric Rocks!");
}
}
}
```
Expand Down