本文主要是介绍springboot day3 配置文件、单元测试、MockMvc,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
1.常见文件格式
xml,properties,json,yml
YAML(Yet Another Markup Language)
2.注解配置⽂件映射属性和实体类
配置文件加载
方式1
1.Controller上⾯配置 @PropertySource({"classpath:resource.properties"})
2.增加属性 @Value("${test.name}")//test.name配置文件里的key
private String name;
方式2 实体类配置⽂件
@Configuration
@ConfigurationProperties(prefix="test")
@PropertySource(value="classpath:test.properties")
public class Test{
}
@Value("${test.test}")private String test;
3 单元测试
<!--springboot程序测试依赖,如果是⾃动创建项⽬默认添加--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope>
</dependency><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.12</version><scope>test</scope>
</dependency
@RunWith(SpringRunner.class) //底层⽤junit SpringJUnit4ClassRunner
@SpringBootTest(classes={AppMain.class})//启动整个springboot⼯程
public class SpringBootTests { }
Controller层登录⽅法测试
@Autowiredprivate TestController testController;@Testpublic void loginTest(){User user = new User();user.setUsername("test");user.setPwd("1234");int relCode = userController.login(user);TestCase.assertEquals(0,relCode );}
表Service层单元测试
@Autowiredprivate TestService testService;@Testpublic void testList(){List<User> userList = testService.listUser();TestCase.assertTrue(userList.size()>0);}
MockMvc调⽤Controller层API接⼝
@Autowiredprivate MockMvc mockMvc;@Test
public void testVideoListApi()throws Exception
{MvcResult mvcResult = mockMvc.perform(MockMvcRequestBuilders.get("/api/pub/test/list"))
.andExpect(MockMvcResultMatchers.status().isOk()).andReturn();int status = mvcResult.getResponse().getStatus();System.out.println(status);//String result = mvcResult.getResponse().getContentAsString();String result = mvcResult.getResponse().getContentAsString(Charset.forName("utf-8"));System.out.println(result);
}
这篇关于springboot day3 配置文件、单元测试、MockMvc的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!