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 9254635
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T11:31:18+00:00 2026-06-18T11:31:18+00:00

Trying to create a constructor that is only called once in my unit tests

  • 0

Trying to create a constructor that is only called once in my unit tests

public class ArtistTest extends InstrumentationTestCase{
    private static final String TAG_NAME = "TESTING_SUITE";
    private TestingMusicDAO musicDAO;
    private List<Song> songs;
    private Instrumentation instr;
    MusicService musicService;

    public ArtistTest() throws Exception {
        super();       
        instr = this.getInstrumentation();
        Log.d(TAG_NAME, "Setting up testing songs");
        musicDAO = new TestingMusicDAO(instr.getContext());
        musicService = new MusicServiceImpl(musicDAO);
        musicDAO.getAllSongsFromFile();
        songs = musicDAO.getAllSongs();
        for(Song song : songs)
            Log.d( TAG_NAME, song.toString() );
    }

But I get an Exception in constructor error when I run the file as a Android Junit Test. Also here is the stack trace

 junit.framework.AssertionFailedError: Exception in constructor: test0      (java.lang.NullPointerException
 at com.intellimec.ilane.ice.mediaservices.ArtistTest.<init>(ArtistTest.java:17)
 at java.lang.reflect.Constructor.constructNative(Native Method)
 at java.lang.reflect.Constructor.newInstance(Constructor.java:417)
 at junit.runner.BaseTestRunner.getTest(BaseTestRunner.java:118)
 at android.test.AndroidTestRunner.getTest(AndroidTestRunner.java:148)
 at android.test.AndroidTestRunner.setTestClassName(AndroidTestRunner.java:56)
 at   android.test.suitebuilder.TestSuiteBuilder.addTestClassByName(TestSuiteBuilder.java:80)
 at android.test.InstrumentationTestRunner.parseTestClass(InstrumentationTestRunner.java:444)
 at android.test.InstrumentationTestRunner.parseTestClasses(InstrumentationTestRunner.java:425)
 at android.test.InstrumentationTestRunner.onCreate(InstrumentationTestRunner.java:370) 
 at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4382)
 at android.app.ActivityThread.access$1300(ActivityThread.java:141)
 at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1294)
 at android.os.Handler.dispatchMessage(Handler.java:99)
 at android.os.Looper.loop(Looper.java:137)
 at android.app.ActivityThread.main(ActivityThread.java:5039)
 at java.lang.reflect.Method.invokeNative(Native Method)
 at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
 at dalvik.system.NativeStart.main(Native Method)
 )
 at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:190)
 at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:175)
 at android.test.InstrumentationTestRunner.onStart(InstrumentationTestRunner.java:555)
 at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1661)
  • 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-18T11:31:19+00:00Added an answer on June 18, 2026 at 11:31 am

    Put the code you only need to execute one time during test runs into static initializer instead. It should only run once, when the class is loaded (do not reuse this class).

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

Sidebar

Related Questions

I'm trying to create a class with a constructor: class MyClass { public int
I am trying to create an SQLite database. The create method: private static class
This might not be possible but I am trying to create a constructor that
Trying to create a widget in the baseclass and the only thing that will
I'm trying to create a class (in C#) that serves as an environment for
I am trying create a delegate representation of constructor by emitting a Dynamic Method,
I'm trying to create a new object of type T via its constructor when
I am trying to create a singleton which has a non-empty constructor and to
I'm trying to create a Database object for myself in .net. The constructor takes
I'm trying to write a class that implements 64-bit ints for a compiler that

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.