how can I use @Enclosed without making methods as static

user389955

I need to have different sets of parameters so I decided to use @Enclosed. However, the nested classes have to be static and therefore all existing methods/constants called by the methods have to be static. But it is hard because I cannot change the definition of all these methods.

Is there any way to use @Enclosed without adding the definition of existing methods with static?

Here is an example so that you know what I am asking. In the example, since data() is static, class someTest has to be static, so if I call nonStaticMethod(), I got "Cannot make a static reference to the non-static method..." at the line calling nonStaticMethod(). But I do not want to redefine nonStaticMethod() to static.

@RunWith(Enclosed.class)
public class EnclosedParameterizedTest extends EnclosedBase{

    @RunWith(Parameterized.class)
    public static class SomeTest {

        @Parameters
        public static Collection<Object[]> data() {
            return Arrays.asList(new Object[][]{
                    new Object[]{"NY"},
                    new Object[]{"CA"},
            });
        }

        String state;

        public SomeTest(String state) {
            this.state = state;
        }

        @Test
        public void verifyStateTest(){
            nonStaticMethod(); //a method already defined in parent of     
            //EnclosedBase which I cannot re-define
        }
    }
}
NamshubWriter

Unfortunately, the Enclosed runner requires that the enclosed classes be static (aka "nested classes").

Edit: in fact, JUnit will never instantiate the class annotated with @RunWith(Enclosed.class). Having a test that uses Enclosed extend another class doesn't do anything unless the base class has fields or methods annotated with JUnit annotations (@ClassRule, @BeforeClass, etc).

I suggest that you share code via delegation, not inheritance. In your case, you can move nonStaticMethod() to a different class:

@RunWith(Enclosed.class)
public class EnclosedParameterizedTest {

    @RunWith(Parameterized.class)
    public static class SomeTest {
        public final Helper helper = new Helper();

        @Parameters
        public static Collection<Object[]> data() {
            return Arrays.asList(new Object[][] {
                    new Object[] { "NY" },
                    new Object[] { "CA" },
            });
        }

        String state;

        public SomeTest(String state) {
            this.state = state;
        }

        @Test
        public void verifyStateTest(){
            helper.nonStaticMethod();
        }
    }
}

In general, delegation is more flexible than inheritance. One common problem with inheritance in Java is that a class can have only one base class. But another problem is that nested classes can't access the outer class's state, so cannot share code other than static methods.

Edit: If that isn't an option, your nested classes can extend your base class:

@RunWith(Enclosed.class)
public class EnclosedParameterizedTest {

    @RunWith(Parameterized.class)
    public static class SomeTest extends EnclosedBase {

        @Parameters
        public static Collection<Object[]> data() {
            return Arrays.asList(new Object[][] {
                    new Object[] { "NY" },
                    new Object[] { "CA" },
            });
        }

        String state;

        public SomeTest(String state) {
            this.state = state;
        }

        @Test
        public void verifyStateTest(){
            nonStaticMethod();
        }
    }
}

Alternatively, if you want different sets of parameters for different tests, consider using the JUnitParams runner:

@RunWith(JUnitParamsRunner.class)
public class MyParameterizedTest extends EnclosedBase {

    @Parameters({"NY", 
                 "CA" })
    @Test
    public void verifyStateTest(String state) {
        nonStaticMethod();
    }
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How can I use round brackets without making a group?

From Dev

How can I use descriptors for non-static methods?

From Dev

How can I use the same generic type in multiple methods in a non-generic static class

From Dev

Can I use Expression.Call on Generic and Static Methods?

From Dev

How can I fill enclosed shapes in a line?

From Dev

How can i open a shell without making it active?

From Dev

How can i open a shell without making it active?

From Dev

How can I use methods of jqLite in AngularJS

From Dev

How can I implement abstract static methods in Java?

From Dev

when use matplotlib for making graph, how can i select range?

From Dev

How can I use tweepy without a filter

From Dev

How can I use CUDA without the monitor?

From Dev

How can I use man without less?

From Dev

How can I use docker without sudo?

From Dev

How can I use percentages without randoms?

From Dev

How do I prevent the simultaneous usage of a static field by the static methods that use it?

From Dev

How do I prevent the simultaneous usage of a static field by the static methods that use it?

From Dev

How can I using Class::Function(params) without static

From Dev

When should I use static methods?

From Dev

Making all DAL methods static

From Dev

Making all DAL methods static

From Dev

I am making a password system thing, how can I make the first IF statement run again without making it into a function?

From Dev

Why can I use 'this' in methods

From Dev

How can I use same methods in different classes type ArrayList?

From Dev

How can I use jQuery promises when referring to methods of an object?

From Dev

How can I use jQuery methods on elements accessed by brackets notation?

From Dev

How can I use functools.singledispatch with instance methods?

From Dev

How can I use the new FormData methods apart from append?

From Dev

How can I use set methods to set data field?

Related Related

  1. 1

    How can I use round brackets without making a group?

  2. 2

    How can I use descriptors for non-static methods?

  3. 3

    How can I use the same generic type in multiple methods in a non-generic static class

  4. 4

    Can I use Expression.Call on Generic and Static Methods?

  5. 5

    How can I fill enclosed shapes in a line?

  6. 6

    How can i open a shell without making it active?

  7. 7

    How can i open a shell without making it active?

  8. 8

    How can I use methods of jqLite in AngularJS

  9. 9

    How can I implement abstract static methods in Java?

  10. 10

    when use matplotlib for making graph, how can i select range?

  11. 11

    How can I use tweepy without a filter

  12. 12

    How can I use CUDA without the monitor?

  13. 13

    How can I use man without less?

  14. 14

    How can I use docker without sudo?

  15. 15

    How can I use percentages without randoms?

  16. 16

    How do I prevent the simultaneous usage of a static field by the static methods that use it?

  17. 17

    How do I prevent the simultaneous usage of a static field by the static methods that use it?

  18. 18

    How can I using Class::Function(params) without static

  19. 19

    When should I use static methods?

  20. 20

    Making all DAL methods static

  21. 21

    Making all DAL methods static

  22. 22

    I am making a password system thing, how can I make the first IF statement run again without making it into a function?

  23. 23

    Why can I use 'this' in methods

  24. 24

    How can I use same methods in different classes type ArrayList?

  25. 25

    How can I use jQuery promises when referring to methods of an object?

  26. 26

    How can I use jQuery methods on elements accessed by brackets notation?

  27. 27

    How can I use functools.singledispatch with instance methods?

  28. 28

    How can I use the new FormData methods apart from append?

  29. 29

    How can I use set methods to set data field?

HotTag

Archive