1. 程式人生 > >SpringBoot測試時拋java.lang.IllegalStateException異常

SpringBoot測試時拋java.lang.IllegalStateException異常

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.再次執行,程式正常。