mauriciotogneri / green-coffee

Green Coffee
MIT License
231 stars 21 forks source link

Would it be possible to use ServiceTestRule #22

Closed funyoung closed 5 years ago

funyoung commented 5 years ago

It is great to deploy with ActivityTestRule, as test for Service classes, is there any suggestion or example?

mauriciotogneri commented 5 years ago

Hi, I'm not sure if I understood your question, but maybe this can help you: https://developer.android.com/training/testing/integration-testing/service-testing

funyoung commented 5 years ago

Thanks for help. Take the example from the link https://developer.android.com/training/testing/integration-testing/service-testing. To work with green-coffee library

  1. Add a feature, service.feature, to assets/
  2. Add a class from GreenCoffeeTest, in which the ServiceTestRule instance is created @RunWith(Parameterized.class) public class ServiceFeatureTest extends GreenCoffeeTest { @Rule public ServiceTestRule service = new ServiceTestRule(); .....
  3. Then, Add a class that extends from GreenCoffeeSteps, in which the feature steps are implemented.

My question is that in some step, it is necessary to access the ServiceTestRule in order to interact with testing target Service, but I am not sure how to do with such task in the step class ServiceTestSteps.

// Verify that the service is working correctly. assertThat(service.getRandomInt()).isAssignableTo(Integer.class);

mauriciotogneri commented 5 years ago

Hi @funyoung,

The way I see it, services are not meant to be tested that way in an end-to-end test. Green Coffee was created to facilitate making tests where you have to interact and check results in the UI. In those tests there should not be a need to check about the internal state of a running service for example.

In any case, if you really want to check the service during a test, Green Coffee doesn't offer any ways to do that. Green Coffee tests runs a normal Instrumentation test so you will have to rely on Android's capability to get access to the service during the test.

In your case it will be more suitable to write a unit test or an integration test (you have an example in the link from my previous message). And that unfortunately is out of the scope of Green Coffee.