SpringBoot 结合Junit4 编写单元测试
发布日期:2021-06-20 05:37:27 浏览次数:8 分类:技术文章

本文共 756 字,大约阅读时间需要 2 分钟。

@RunWith(SpringJUnit4ClassRunner.class)@SpringBootTest(classes = Application.class) //测试类最好放在主包(带有Application类的包里面)public class MetricsTest {    private MockMvc mockMvc;  //没有使用@Component 注解的类 需要使用new 自己创建    @Autowired    ObjectMapper objectMapper;    // 使用@Component 注解的类,只能通过注入的方式引用(因为Spring默认是单例模式)    @Before    public void setup() throws Exception {        mockMvc = standaloneSetup(new MetricAPI()).build();      }    /**     * Adds a valid metric     * @throws Exception      */    @Test    public void addMetricTest() throws Exception{        mockMvc.perform(post("/addMetric")            .contentType(MediaType.APPLICATION_JSON)            .content(objectMapper.writeValueAsString(new Metric())))                .andExpect(status().isOk());    }}

 

转载地址:https://blog.csdn.net/h2453532874/article/details/91042776 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:Windows 10 文件名太长 无法删除 复制 重命名
下一篇:@Resource 和 @Autowire 的区别

发表评论

最新留言

留言是一种美德,欢迎回访!
[***.207.175.100]2024年04月04日 19时18分26秒