1.错误信息
org.junit.runners.model.InvalidTestClassError: Invalid test class 'com.bcl.demo1.control.TestGetToken':
1. No runnable methods
java.lang.NullPointerException
at com.bcl.demo1.control.TestGetToken
2.解决方案:@test注解包引入错误,需引入第二个包的注解
import org.junit.jupiter.api.Test;
//
import org.junit.Test;
3.测试代码
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
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.result.MockMvcResultHandlers;
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
@RunWith(SpringRunner.class)
@SpringBootTest
public class TestGetToken {
@Autowired
private GetToken2Person getToken2Person;
private MockMvc mockMvc;
@Before
public void setup(){
mockMvc = MockMvcBuilders.standaloneSetup(getToken2Person).build();
}
@Test
public void getToken() throws Exception {
mockMvc.perform(MockMvcRequestBuilders.get("/findAll"))
.andExpect(MockMvcResultMatchers.status().isOk())
.andDo(MockMvcResultHandlers.print())
.andReturn();
}
}