Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8231453
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T17:29:51+00:00 2026-06-07T17:29:51+00:00

I have simple ServiceTestCase and an empty IntentSerice containing life-cycle methods only, the Service

  • 0

I have simple ServiceTestCase and an empty IntentSerice containing life-cycle methods only, the Service is registered AndroidManifest.xml also, as well as an Action tag “com.tut.my.service.DATAUPDATE”.

The calling of Service works pretty well, all live-cycle methods appears in order, but something strange happens after the finish of my custom test. ServiceTestCase issued the method testServiceTestCaseSetUpProperly() AFTER my custom test to ensure that setupService() runs correctly.

So I’m looking arround and find some interesting blog concern setupService(), but the conclusion was not satisfy. The blog author advises – for some good reason – not to call startService(…) in setUp(), as made himself abundantly clear why it’s not a good idea though.

However, the problem that arose for me from calling testServiceTestCaseSetUpProperly() after my custom test is the call for a new instance of MyService service. This ends up invoking onCreate and than
the Service dies for some reason, finito… but all tests passes successfully.

Here is the source for the Service:

public class MyService extends IntentService {

  public static final String INTENT = "com.tut.my.service.DATAUPDATE";

  public MyService() {
    super(INTENT);
    Log.d(getClass().getSimpleName(), "called: std c-tor()");
  }

  @Override
  public void onCreate() {
    super.onCreate();
    Log.d(getClass().getSimpleName(), "called: onCreate()");
  }

  @Override
  public int onStartCommand(Intent intent, int flags, int startId) {
    Log.d(getClass().getSimpleName(), "called onStartCommand() --> intent: " + intent.getAction() + " flags: " + flags + " startID: " + startId);
    return super.onStartCommand(intent, flags, startId);
  }

  @Override
  public void onDestroy() {
    super.onDestroy();
    Log.d(getClass().getSimpleName(),"called: onDestory()");
  }

  @Override
  protected void onHandleIntent(Intent intent) {
    Log.d(getClass().getSimpleName(), "called: onHandleIntent() --> Intent: " + intent.getAction());
    setPriceData();
  }
}

Here is the source for ServiceTestCase

@MediumTest
public class MyServiceTest extends ServiceTestCase<MyService> {

  Context mCtx = null;

  public MyServiceTest() {
    super(MyService.class);
  }

  @Override
  protected void setUp() throws Exception {
    super.setUp();
    mCtx = getSystemContext();
  }

  @MediumTest
  public void testGetMyServiceData() throws InterruptedException {
    Intent i = new Intent(MyService.INTENT);
    Log.d(getClass().getSimpleName(), "startService(i)");
    mCtx.startService(i);

    // throttle the instrumentation thread so the service 
    // can be instantiated for sure
    Log.d(getClass().getSimpleName(), "before Thread Sleep");
    Thread.sleep(8000); // needs 10 seconds before ANR
    Log.d(getClass().getSimpleName(), "after Thread Sleep");
  }
}

And the corresponding (stripped) LogCat output:

I/TestRunner(11954): started: testGetMyServiceData(com.tut.my.service.MyServiceTest)
D/MyServiceTest(11954): startService(i)    
D/MyService(11954): called: std c-tor()
D/MyServiceTest(11954): before Thread Sleep
D/MyService(11954): called: onCreate()
D/MyService(11954): called onStartCommand() --> intent: com.tut.my.service.DATAUPDATE flags: 0 startID: 1
D/MyService(11954): called: onHandleIntent() --> Intent: com.tut.my.service.DATAUPDATE
D/MyService(11954): called: onDestory()
D/MyServiceTest(11954): after Thread Sleep
I/TestRunner(11954): finished: testGetMyServiceData(com.tut.my.service.MyServiceTest)
I/TestRunner(11954): passed: testGetHarvestData(com.tut.my.service.MyServiceTest)
I/TestRunner(11954): started: testServiceTestCaseSetUpProperly(com.tut.my.service.MyServiceTest)
D/MyService(11954): called: std c-tor()
I/TestRunner(11954): finished: testServiceTestCaseSetUpProperly(com.tut.my.service.MyServiceTest)
I/TestRunner(11954): passed: testServiceTestCaseSetUpProperly(com.tut.my.service.MyServiceTest)
I/TestRunner(11954): started: testAndroidTestCaseSetupProperly(com.tut.my.service.MyServiceTest)
I/TestRunner(11954): finished: testAndroidTestCaseSetupProperly(com.tut.my.service.MyServiceTest)
I/TestRunner(11954): passed: testAndroidTestCaseSetupProperly(com.tut.my.service.MyServiceTest)
I/ActivityManager(71): Force stopping package com.tut.my.service uid=10036
I/Process(71): Sending signal. PID: 11954 SIG: 9

So the question is, why testServiceTestCaseSetupProperly() is called at the end and not in the first place? And why MyService object dies so ungracefully.

EDIT : To make it more precise:

The important part of my concern is the rude call to onCreate.

 I/TestRunner(11954): started: testServiceTestCaseSetUpProperly(com.tut.my.service.MyServiceTest)
 D/MyService(11954): called: std c-tor() <--- ONLY one call to onCreate
 I/TestRunner(11954): finished:testServiceTestCaseSetUpProperly(com.tut.my.service.MyServiceTest)
  • If im going to allocate resources in onCreate, who asserts me to clean up the allocated resrouces?
  • An why the check for proper service setup starts at the end of all tests?
  • And why it get’s killed anyway?
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T17:29:53+00:00Added an answer on June 7, 2026 at 5:29 pm

    JUnit 3 uses reflection to obtain the tests to run. It is not guaranteed that tests are run in any specific order.
    Additionally, unit tests should be independent of each other so their execution order should not be relevant.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have simple win service, that executes few tasks periodically. How should I pass
Have simple Spring Security webapp with password encoding: <security:authentication-manager alias=authenticationManager> <security:authentication-provider user-service-ref=personService> <security:password-encoder hash=md5
i have simple form that uploads image to server and returns php string containing
I have simple WCF Service Application (based on this tutorial : Getting Started ).
I have simple test (WPF - MVVM) client (for WCF oData service) app: ViewModel
I have simple methods to export DataTable to XLS using string. Number of columns
Have simple , empty project without any code. just includes. The include linux/netfilter.h cause
i have simple xsd file: <?xml version=1.0?> <xs:schema xmlns:xs=http://www.w3.org/2001/XMLSchema> <xs:element name=note> <xs:complexType> <xs:sequence> <xs:element
I have simple static lib in xcode with the only class test.h: @interface TestClass
I have simple table in Sybase -- Creating table 'SimpleText' CREATE TABLE [dbo].[SimpleText] (

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.