1

This is hopefully a simple question.

I have a Spring Boot Test.

The annotations are:

@RunWith(SpringRunner.class)
@SpringBootTest(classes = Main.class)
@TestPropertySource(locations { "myproperties.properties" })

I've got a test that I'd love to use @RunWith(Theories.class). The test is testing basically the same thing over several different places in my code.

Of course, @RunWith must be singular.

So is there a way to have the theories started up as a rule? Or SpringRunner.class? Or something so that they can co-exist?

newbo
  • 163
  • 1
  • 9

1 Answers1

0

There should be two ways to solve that:

  1. SpringClassRule and SpringMethodRule <-- I use this approach

    just use

    @RunWith(Theories.class)
    public class YourTest {
        @ClassRule
        public static final SpringClassRule scr = new SpringClassRule();
    
        @Rule
        public final SpringMethodRule smr = new SpringMethodRule()
    
  2. Initializing the TestContextManager Manually according to Baeldung it works for Parameterized, but I wouldn't know why it shouldn't work for Theories also ;) Generally, it is not recommended to initialize the TestContextManager manually. Instead, Spring recommends using SpringClassRule and SpringMethodRule.

    @RunWith(Parameterized.class)
    public class YourTest {
    
        private TestContextManager testContextManager;
        @Before
        public void setup() throws Exception {
            this.testContextManager = new TestContextManager(getClass());
            this.testContextManager.prepareTestInstance(this);
        }
    
Haphil
  • 1,180
  • 1
  • 14
  • 33