zoukankan      html  css  js  c++  java
  • SPRING BOOT 15.1 TEST

    package example;
    
    import org.junit.Assert;
    import org.junit.Before;
    import org.junit.Test;
    import org.junit.runner.RunWith;
    import org.mockito.Matchers;
    import org.mybatis.spring.boot.autoconfigure.MybatisAutoConfiguration;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
    import org.springframework.boot.test.context.SpringBootTest;
    import org.springframework.http.MediaType;
    import org.springframework.test.context.junit4.SpringRunner;
    import org.springframework.test.context.web.WebAppConfiguration;
    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;
    import org.springframework.web.context.WebApplicationContext;
    
    import com.google.gson.GsonBuilder;
    
    import html.example.SpringDemoApplication;
    import html.example.portal.data.MemberDto;
    import html.example.portal.data.MemberEntity;
    
    @WebAppConfiguration
    @RunWith(SpringRunner.class)
    @SpringBootTest(classes = { SpringDemoApplication.class, DataSourceAutoConfiguration.class,
            MybatisAutoConfiguration.class })
    public class MemberControllerTest {
    
        @Autowired
        protected WebApplicationContext context;
    
        private MockMvc mockMvc;
    
        @Before
        public void before() {
            // mockMvc = MockMvcBuilders.standaloneSetup(memberService).build();
            mockMvc = MockMvcBuilders.webAppContextSetup(context).build();// 建议使用这种
        }
    
        @Test
        public void testAddUser() throws Exception {
            MemberDto dto = new MemberDto();
            dto.setEmail("jis117@yahoo.com");
            dto.setStatus(0);
    
            String content = new GsonBuilder().create().toJson(dto);
            mockMvc.perform(MockMvcRequestBuilders.post("/member/add").contentType(MediaType.APPLICATION_JSON_UTF8)
                    .content(content).
                    // param("username", "123.123").
                    // param("password", "456.456").
                    // param("status", "0").
                    accept(MediaType.APPLICATION_JSON)).andExpect(MockMvcResultMatchers.status().isOk())
                    .andDo(MockMvcResultHandlers.print())
                    .andExpect(MockMvcResultMatchers.content().string(Matchers.contains("succ")));
    
        }
    
        public void test() {
            MemberEntity member = new MemberEntity();
            Assert.assertNotNull(member.getId());
        }
    
    }
  • 相关阅读:
    分期付款购买固定资产账务处理
    会计要素计量
    接受现金捐赠分录
    分配股票股利的分录
    R语言代写对用电负荷时间序列数据进行K-medoids聚类建模和GAM回归
    R语言代写用随机森林和文本挖掘提高航空公司客户满意度
    R语言代写时间序列TAR阈值模型分析 2
    R语言代写时间序列TAR阈值模型分析
    R语言代写文本挖掘tf-idf,主题建模,情感分析,n-gram建模研究
    R语言代写文本挖掘NASA数据网络分析,tf-idf和主题建模
  • 原文地址:https://www.cnblogs.com/jpit/p/7442229.html
Copyright © 2011-2022 走看看