Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 432
Artur Avatar asked Dec 31 '25 23:12

Artur


1 Answers

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.

like image 68
Igor Deruga Avatar answered Jan 02 '26 12:01

Igor Deruga