1
votes

So I have this error, using Robolectric.

java.lang.IllegalArgumentException: Test class can only have one constructor
    at org.junit.runners.model.TestClass.(TestClass.java:40)
    at org.junit.runners.ParentRunner.(ParentRunner.java:75)
    at org.junit.runners.BlockJUnit4ClassRunner.(BlockJUnit4ClassRunner.java:57)
    at org.robolectric.RobolectricTestRunner$HelperTestRunner.(RobolectricTestRunner.java:626)
    at org.robolectric.RobolectricTestRunner$2.evaluate(RobolectricTestRunner.java:198)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
    at org.robolectric.RobolectricTestRunner$1.evaluate(RobolectricTestRunner.java:177)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
    at org.apache.maven.surefire.junit4.JUnit4Provider.execute(JUnit4Provider.java:264)
    at org.apache.maven.surefire.junit4.JUnit4Provider.executeTestSet(JUnit4Provider.java:153)
    at org.apache.maven.surefire.junit4.JUnit4Provider.invoke(JUnit4Provider.java:124)
    at org.apache.maven.surefire.booter.ForkedBooter.invokeProviderInSameClassLoader(ForkedBooter.java:200)
    at org.apache.maven.surefire.booter.ForkedBooter.runSuitesInProcess(ForkedBooter.java:153)
    at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:103)

To be honest, it worked weeks ago, but after few changes in my project it broke. I'm using 4.3_r1 platform version. Normal JUnit tests work well.

The test class:

@RunWith(RobolectricTestRunner.class)
public class PreferenceTest
{
    @Before
    public void setUp()
    {
        RoboGuice.setBaseApplicationInjector(Robolectric.application, RoboGuice.DEFAULT_STAGE,
                Modules.override(RoboGuice.newDefaultRoboModule(Robolectric.application))
                        .with(new CoreConfigModule()));
    }

    @Test
    public void testSessionTokenPrefs()
    {
        InternalPreferences.setSessionToken("abcd1234");
        assertThat(InternalPreferences.getSessionToken()).isEqualTo("abcd1234");

        InternalPreferences.clearSessionToken();
        assertThat(InternalPreferences.getSessionToken()).isNull();
    }
}
3
I'm not sure. Robolectric works directly with class loader. Maybe this combination (Robolectric & RoboGuice) adds another constructor to classEugen Martynov

3 Answers

1
votes

So from the svn history, it seemed that the test broke after I renamed the test package.

I've just renamed it again, but not to it's previous name, and its working again. I don't know what's going on exactly, but at least my tests are running.

0
votes

It seems like You're passing the class junit.framework.TestSuite.class to JUnitCore.runClasses(). JUnitCore.runClasses tries to instantiate the class. To avoid problems, it requires that any class that you pass has only one constructor

You can also check these discussion for more details, link1,link2,link3

0
votes

In my case it happens because I put duplicated words in my package name. It is something like :

au.com.andoroid.com.android

Once I renamed this package with something that does not has duplication then it works fine.