单元测试

1、SpringBoot Control单元测试
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = {Application.class})
@AutoConfigureMockMvc
public class RyZdryPzZrqControllerTest {
    @Autowired
private MockMvc mockMvc;

    @Test
public void testUpdateDeptName() throws Exception {
        ResultActions result = mockMvc.perform(MockMvcRequestBuilders.post("/xtpz/zrqpz/updateDeptName").contentType(MediaType.APPLICATION_FORM_URLENCODED).param("code", "320584610013").param("name", "测试A"));
        MvcResult mvcResult= result.andReturn();
        ModelAndView modelAndView = mvcResult.getModelAndView();
        System.out.println(modelAndView.toString());
}
}
2、Spring Junit测试
@RunWith(SpringJUnit4ClassRunner.class) 
@ContextConfiguration("/config/Spring-db1.xml") 
 @Transactional 
 public class AccountServiceTest1 { 
     @Autowired 
     private AccountService service; 
    
     @Test 
     public void testGetAcccountById() { 
 Account acct = Account.getAccount(1, "user01", 18, "M"); 
         service.insertIfNotExist(acct); 
         Account acct2 = service.getAccountById(1); 
         assertEquals(acct,acct2); 
     }
3、Spring手动加载环境
  ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
        
        TopProducer topProducer = (TopProducer)context.getBean("topProducer");
        
        topProducer.send("lilixin", "this ia a kafka test msg");

相关推荐