0

I have a class with a nested class, which contains test case. It looks like this:

public class ProviderUtilsTest {
        public static final String TAG = ProviderUtilsTest.class.getSimpleName();

        public static class TestConstructor {

            @Test
            public void noPublicConstructor() {
                Class<ProviderUtils> clazz = ProviderUtils.class;
                Constructor<?>[] constructors = clazz.getConstructors();
                for (Constructor<?> constructor : constructors) {
                    assertThat(constructor.isAccessible(), is(false));
                }
            }
        }
    }

When I use this configuration Android Studio doesn't treat ProviderUtilsTest as a test case. I.e. it doesn't allow to run it when I click on its file with RMB and it doesn't display run button on the left of the class declaration line. But it displays it for nested class.

How can I make it treat the ProviderUtilsTest as a test case i.e. display an appropriate UI alements and force it to run all tests of the nested classes?

piotrek1543
  • 19,130
  • 7
  • 81
  • 94
Lingviston
  • 5,479
  • 5
  • 35
  • 67
  • 4
    Why do you want to put tests in nested classes? – J Fabian Meier Sep 02 '16 at 14:40
  • For the better structure purposes. I want the outer class to represent the test case for the whole class, each nested class a test case for aprticular method, each method of nested class to be a test for specific input. – Lingviston Sep 02 '16 at 16:47
  • I doubt that this is a good approach. If you nest the classes purely for structure, think about using packages, subpackages and naming conventions. Furthermore you can write helper classes that gather initialization logic if you don't want to duplicate such things. – J Fabian Meier Sep 02 '16 at 18:40
  • To test the package visible methods I need to put all the test cases in the same package. Considering this and that fact that I want tests for a method to be in a separate class - the package is going to grow very fast. – Lingviston Sep 03 '16 at 16:06

2 Answers2

0

I don't want to say how bad your implementation of good test idea is, but I would provide you my solution, which should work and give the same result as yours.

Change your existing code to:

public class ProviderUtilsTest {
        public static final String TAG = ProviderUtilsTest.class.getSimpleName();

        public static class TestConstructor {

            public boolean noPublicConstructor() {
                Class<ProviderUtils> clazz = ProviderUtils.class;
                Constructor<?>[] constructors = clazz.getConstructors();
                for (Constructor<?> constructor : constructors) {
                    if (constructor.isAccessible()) return false;
                }
                return true
            }
        }
    }

Then in your ExampleActivityTest write this:

 @Test
 public void ensureThatThereIsNoPublicConstructor() throws Exception {
    assertTrue(ProviderUtilsTest.TestConstructor.noPublicConstructor());
}

Hope it will help

piotrek1543
  • 19,130
  • 7
  • 81
  • 94
0

For the better structure purposes. I want the outer class to represent the test case for the whole class, each nested class a test case for aprticular method, each method of nested class to be a test for specific input

Alternatively, you can use packages to organize your test cases.

Code-Apprentice
  • 81,660
  • 23
  • 145
  • 268
  • I need to put test cases in the same package as the class under testing. Which means that test package will grow very quickly if I want to have a separate class for all tests of a single method. – Lingviston Sep 04 '16 at 12:14
  • "I need to put test cases in the same package as the class under testing." What are your reasons for this? Are you testing package-private methods? – Code-Apprentice Sep 06 '16 at 16:54
  • Yes. We have some helper methods within many classes which are marked as @VisibleForTesting and made package private. – Lingviston Sep 08 '16 at 11:02