其他分享
首页 > 其他分享> > springboot实战原理分析笔记(十七)-spring boot测试步骤

springboot实战原理分析笔记(十七)-spring boot测试步骤

作者:互联网

spring boot测试步骤
1.直接在测试类上面加2个注解

@RunWith(SpringRunner.class)
@SpringBootTest(classes = UserDao.class)

只在测试环境下有效的 @TestConfiguration

springboot会有限加载测试环境的配置文件,测试环境没有才会加载正式环境
设置配置还可以通过一下方法:
1.

@SpringBootTest(classes = App.class, properties = {"springboot.version=1.0.0"})
@Autowired
    private ConfigurableApplicationContext configurableApplicationContext;

    @org.junit.Before
    public void before(){
        EnvironmentTestUtils.addEnvironment(configurableApplicationContext, "aaa=aaaw");
    }

//只有接口的没有实现方法,可以通过一下方法预测

@RunWith(SpringRunner.class)
public class UserMapperTest {

    @MockBean
    private UserMapper userMapper;


    @Before
    public void init(){
        //通过这个可以预测
        BDDMockito.given(userMapper.createUser("name")).willReturn(Integer.valueOf(1));
        BDDMockito.given(userMapper.createUser("")).willReturn(Integer.valueOf(0));
        BDDMockito.given(userMapper.createUser(null)).willThrow(new NullPointerException());
    }

    @Test(expected = NullPointerException.class)
    public void testEnv(){
        Assert.assertEquals( Integer.valueOf(1),userMapper.createUser("name"));
        Assert.assertEquals( Integer.valueOf(0),userMapper.createUser(""));
        Assert.assertEquals( new NullPointerException(),userMapper.createUser(null));
    }
}

测试controller: TestRestTemplate 需要运行在web环境中
第一种方法:

@RunWith(SpringRunner.class)
@SpringBootTest(classes = App.class, webEnvironment=SpringBootTest.WebEnvironment.DEFINED_PORT)
public class BookControllerTest { 


    @Autowired
    private TestRestTemplate testRestTemplate;
    @Test
    public void testHome() throws Exception {
        String forObject = testRestTemplate.getForObject("/book/home", String.class);
        Assert.assertEquals("book home", forObject);
    }


    @Test
    public void testShow() throws Exception {
        String forObject = testRestTemplate.getForObject("/book/show?id=1", String.class);
        Assert.assertEquals("book1", forObject);
    }
} 

@WebMvcTest(controllers = BookController.class) 不需要运行在web环境下,但是需要制定controllers,表示需要测试那些controllers
这种方式只测试controller,controller里面的一些依赖,需要自己去mock
@WebMvcTest不加载整个spring容器
第二种方法():

@RunWith(SpringRunner.class)
@WebMvcTest(controllers = BookController.class )
@ContextConfiguration(classes = App.class)
public class BookControllerTest2 {

    @Autowired
    private MockMvc mvc;

    @Test
    public void testHome() throws Exception {
        mvc.perform(MockMvcRequestBuilders.get("/book/home")).andExpect(MockMvcResultMatchers.status().isOk());
        mvc.perform(MockMvcRequestBuilders.get("/book/home")).andExpect(MockMvcResultMatchers.status().isOk()).andExpect(MockMvcResultMatchers.content().string("book home"));    }


    @Test
    public void testShow() throws Exception {
        mvc.perform(MockMvcRequestBuilders.get("/book/show").param("id","123")).andExpect(MockMvcResultMatchers.status().isOk()).andExpect(MockMvcResultMatchers.content().string("book123"));

    }
} 

@WebMvcTest不能和@SpringBootTest一起使用,需要加载整个spring容器,使用下面的方式
如果需要使用MockMvc对象,加上@AutoConfigureMockMvc注解

@RunWith(SpringRunner.class)
@SpringBootTest(classes = App.class)
@AutoConfigureMockMvc
public class BookControllerTest2 {

    @Autowired
    private MockMvc mvc;

    @Test
    public void testHome() throws Exception {
        mvc.perform(MockMvcRequestBuilders.get("/book/home")).andExpect(MockMvcResultMatchers.status().isOk());
        mvc.perform(MockMvcRequestBuilders.get("/book/home")).andExpect(MockMvcResultMatchers.status().isOk()).andExpect(MockMvcResultMatchers.content().string("book home"));    }


    @Test
    public void testShow() throws Exception {
        mvc.perform(MockMvcRequestBuilders.get("/book/show").param("id","123")).andExpect(MockMvcResultMatchers.status().isOk()).andExpect(MockMvcResultMatchers.content().string("book123"));

    }
} 	

标签:springboot,spring,void,测试步骤,class,book,andExpect,public,MockMvcResultMatchers
来源: https://blog.csdn.net/fsdf8sad7/article/details/96000529