SpringBoot测试时抛java.lang.IllegalStateException异常

版权声明:转载请附上博客地址,谢谢~ https://blog.csdn.net/ruren1/article/details/84107105

1.我的测试类


@SpringBootTest
@RunWith(SpringRunner.class)
public class MaServiceTest {



    @Autowired
    private MaService maService;


    @Test
    public void delete() {

        maService.delete(1062530611807285249L);

    }
}

2.运行时的异常

java.lang.IllegalStateException: Unable to find a @SpringBootConfiguration, you need to use @ContextConfiguration or @SpringBootTest(classes=...) with your test

3.解决办法

在@SpringBootTest上加入项目中的启动类,我的程序的启动类是NuocheServerApplication.java。对上面的测试类进行修改,得到:


@SpringBootTest(classes = NuocheServerApplication.class) // 修改部分
@RunWith(SpringRunner.class)
public class MaServiceTest {



    @Autowired
    private MaService maService;


    @Test
    public void delete() {

        maService.delete(1062530611807285249L);

    }
}

4.再次运行,程序正常。

猜你喜欢

转载自blog.csdn.net/ruren1/article/details/84107105