springboot測試時 SpringApplicationConfiguration注解不能用


測試時,@SpringApplicationConfiguration(classes = Application.class) 報錯,注解不能導入。

在學習spring boot時,按照文檔學習時測試時,我也是遇到這個問題,看了好多資料,有的說是這個注解在1.4就被替換了,我用的1.5.2版本的,直接用自動生成的兩個注解就可以實現測試功能。
@RunWith(SpringRunner.class)
@SpringBootTest


測試代碼:

package com.didispace.hello;

import static org.hamcrest.Matchers.equalTo;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;

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

//模擬調用controller接口發起請求
private MockMvc mvc;

@Before
public void setUp(){
mvc = MockMvcBuilders.standaloneSetup(new HelloController()).build();
}

@Test
public void hello() throws Exception{
mvc.perform(MockMvcRequestBuilders.get("/hello")
.accept(org.springframework.http.MediaType.APPLICATION_JSON)
).andExpect(content().string(equalTo("hello")))
.andExpect(status().isOk());
}
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM