Artur Artur -4 years ago 122
Java Question

Custom annotation on test

I have some test that need Spring context to run:

@SpringBootTest
@ContextConfiguration(classes = Application.class)
@Transactional
@ActiveProfiles("test")
@Rollback
@RunWith(SpringRunner.class)
public class SomeTest() {

@Test
public void test() {
...
}

}


I have created custom annotation for test:

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Inherited
@SpringBootTest
@ContextConfiguration(classes = Application.class)
@Transactional
@ActiveProfiles("test")
@Rollback
@RunWith(SpringRunner.class)
public @interface DBTest {
}


Now - when i use @DBTest annotation on my test:

@DBTest
public class SomeTest {

@Test
public void test() {
...
}

}


In this case Spring context was not started.
How can I start that?

Answer Source

Try putting @RunWith(SpringRunner.class) in your SomeTest class annotations. In order to apply the annotations you need spring to analyse them. But since your RunWith is not on your class, jUnit framework doesn't start spring, which, in turn, doesn't analyse the annotations.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download