Commit fb8f8b7d authored by 技术部-任文超's avatar 技术部-任文超

更新所有Demo(在非Apollo项目中已全部执行成功),需要继续完成本地化(进行中)

parent 3a3a3c4f
package demo;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.DynamicContainer.dynamicContainer;
import static org.junit.jupiter.api.DynamicTest.dynamicTest;
import org.junit.jupiter.api.*;
import org.junit.jupiter.api.function.ThrowingConsumer;
import java.util.Arrays;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.Random;
import java.util.*;
import java.util.function.Function;
import java.util.stream.IntStream;
import java.util.stream.Stream;
import org.junit.jupiter.api.*;
import org.junit.jupiter.api.function.ThrowingConsumer;
import static org.junit.jupiter.api.Assertions.*;
import static org.junit.jupiter.api.DynamicContainer.dynamicContainer;
import static org.junit.jupiter.api.DynamicTest.dynamicTest;
/**
......
package demo;
import cn.quantgroup.xyqb.Constants;
import cn.quantgroup.xyqb.controller.internal.user.UserController;
import cn.quantgroup.xyqb.entity.User;
import cn.quantgroup.xyqb.service.user.IUserService;
import jdk.nashorn.internal.runtime.regexp.joni.Config;
import org.json.JSONObject;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.boot.test.context.ConfigFileApplicationContextInitializer;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.http.MediaType;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.MvcResult;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import static org.mockito.BDDMockito.given;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
@ContextConfiguration(classes = Config.class, initializers = ConfigFileApplicationContextInitializer.class)
@RunWith(SpringRunner.class)
@WebMvcTest({UserController.class})
public class MockMvcTests {
@Autowired
private MockMvc mvc;
@MockBean
private IUserService userService;
/**
* 测试Server是否可达
* @throws Exception
*/
@Test
public void testServer() throws Exception {
mvc.perform(MockMvcRequestBuilders.get("/").accept(MediaType.APPLICATION_JSON))
.andExpect(status().isOk());
}
@Test
public void mvcTest() throws Exception {
String uri = "/lock/key";
MvcResult mvcResult = mvc.perform(MockMvcRequestBuilders.get(uri).accept(MediaType.APPLICATION_JSON)
.header("lock_ipv4", Constants.CLEAR_LOCK_FOR_IPV4)
.param("act", Boolean.TRUE.toString()))
.andExpect(status().isOk())
.andReturn();
String content = mvcResult.getResponse().getContentAsString();
JSONObject jsonResult = new JSONObject(content);
Object code = jsonResult.get("code");
Assert.assertEquals("0000", code);
Object data = jsonResult.get("data");
Assert.assertNotNull(data);
}
@Test
public void testExample() throws Exception {
String phone = "13461067662";
User user = new User();
user.setPhoneNo(phone);
given(userService.findByPhoneInDb(phone)).willReturn(user);
mvc.perform(get("/log/list").accept(MediaType.TEXT_PLAIN))
.andExpect(status().isOk()).andExpect(content().string("Honda Civic"));
}
}
package demo;
import cn.quantgroup.xyqb.Bootstrap;
import cn.quantgroup.xyqb.Constants;
import com.fasterxml.jackson.core.JsonProcessingException;
import org.json.JSONObject;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.MediaType;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.MvcResult;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = Bootstrap.class)
@WebAppConfiguration
public class MvcTests {
private MockMvc mvc;
@Autowired
WebApplicationContext webApplicationConnect;
@Before
public void setUp() throws JsonProcessingException {
mvc = MockMvcBuilders.webAppContextSetup(webApplicationConnect).build();
}
/**
* 测试Server是否可达
* @throws Exception
*/
@Test
public void testServer() throws Exception {
mvc.perform(MockMvcRequestBuilders.get("/").accept(MediaType.APPLICATION_JSON))
.andExpect(status().isOk());
}
@Test
public void mvcTest() throws Exception {
String uri = "/lock/key";
MvcResult mvcResult = mvc.perform(MockMvcRequestBuilders.get(uri).accept(MediaType.APPLICATION_JSON)
.header("lock_ipv4", Constants.CLEAR_LOCK_FOR_IPV4)
.param("act", Boolean.TRUE.toString()))
.andExpect(status().isOk())
.andReturn();
String content = mvcResult.getResponse().getContentAsString();
JSONObject jsonResult = new JSONObject(content);
Object code = jsonResult.get("code");
Assert.assertEquals("0000", code);
Object data = jsonResult.get("data");
Assert.assertNotNull(data);
}
}
package demo;
import cn.quantgroup.xyqb.entity.User;
import cn.quantgroup.xyqb.repository.IUserRepository;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase;
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
import org.springframework.boot.test.autoconfigure.orm.jpa.TestEntityManager;
import org.springframework.test.annotation.Rollback;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.transaction.annotation.Transactional;
import javax.persistence.PersistenceException;
import java.sql.Timestamp;
import java.time.Instant;
/**
* 持久层测试用例
* @author renwc
* @date 2018-01-05
*/
@Rollback
@Transactional
@RunWith(SpringRunner.class)
@DataJpaTest
@AutoConfigureTestDatabase(replace= AutoConfigureTestDatabase.Replace.NONE)
public class RepsitoryJpaTests {
@Autowired
private TestEntityManager entityManager;
@Autowired
private IUserRepository userRepository;
@Test(expected = PersistenceException.class)
public void testSaveExample() throws Exception {
String phone = "13461067662";
User user = new User();
user = userRepository.save(user);
User userRep = userRepository.findByPhoneNo(phone);
Assert.assertEquals(user, userRep);
}
@Test
public void testExample() throws Exception {
String phone = "13461067662";
String password = "318e235d3e52648b236faa3f748000d5";//123456
User user = new User();
user.setPhoneNo(phone);
user.setPassword(password);
user.setCreatedAt(Timestamp.from(Instant.now()));
user.setUpdatedAt(user.getCreatedAt());
user = userRepository.save(user);
User userRep = userRepository.findByPhoneNo(phone);
Assert.assertEquals(user, userRep);
}
}
package demo;
import cn.quantgroup.xyqb.entity.User;
import cn.quantgroup.xyqb.repository.IUserRepository;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.annotation.Rollback;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.transaction.annotation.Transactional;
import javax.persistence.PersistenceException;
import java.sql.Timestamp;
import java.time.Instant;
/**
* 持久层测试用例
* @author renwc
* @date 2018-01-05
*/
@Rollback
@Transactional
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment= SpringBootTest.WebEnvironment.RANDOM_PORT)
public class RepsitoryTests {
@Autowired
private IUserRepository userRepository;
@Test(expected = PersistenceException.class)
public void testSaveExample() throws Exception {
String phone = "13461067662";
User user = new User();
user = userRepository.save(user);
User userRep = userRepository.findByPhoneNo(phone);
Assert.assertEquals(user, userRep);
}
@Test
public void testInsertExample() throws Exception {
String phone = "13461067662";
String password = "318e235d3e52648b236faa3f748000d5";//123456
User user = new User();
user.setPhoneNo(phone);
user.setPassword(password);
user.setCreatedAt(Timestamp.from(Instant.now()));
user.setUpdatedAt(user.getCreatedAt());
user = userRepository.save(user);
User userRep = userRepository.findByPhoneNo(phone);
Assert.assertEquals(user, userRep);
}
}
package demo;
import cn.quantgroup.xyqb.entity.User;
import cn.quantgroup.xyqb.service.user.IUserService;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.annotation.Rollback;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.transaction.annotation.Transactional;
import javax.persistence.PersistenceException;
import java.sql.Timestamp;
import java.time.Instant;
/**
* 业务层测试用例
* @author renwc
* @date 2018-01-05
*/
@Rollback
@Transactional
@RunWith(SpringRunner.class)
@SpringBootTest
@ContextConfiguration
public class ServiceTests {
@Autowired
private IUserService userService;
@Test(expected = PersistenceException.class)
public void testSaveExample() throws Exception {
String phone = "13461067662";
User user = new User();
user = userService.saveUser(user);
User userRep = userService.findByPhoneInDb(phone);
Assert.assertEquals(user, userRep);
}
@Test
public void testInsertExample() throws Exception {
String phone = "13461067662";
String password = "318e235d3e52648b236faa3f748000d5";//123456
User user = new User();
user.setPhoneNo(phone);
user.setPassword(password);
user.setCreatedAt(Timestamp.from(Instant.now()));
user.setUpdatedAt(user.getCreatedAt());
user = userService.saveUser(user);
User userRep = userService.findByPhoneInDb(phone);
Assert.assertEquals(user, userRep);
}
}
package demo;
import cn.quantgroup.xyqb.Bootstrap;
import com.ctrip.framework.apollo.spring.config.ApolloPropertySourceInitializer;
import jdk.nashorn.internal.runtime.regexp.joni.Config;
import lombok.extern.slf4j.Slf4j;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.ConfigFileApplicationContextInitializer;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
import org.springframework.boot.test.web.client.TestRestTemplate;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringRunner;
/**
* 控制层测试用例
* @author renwc
* @date 2018-01-05
*/
//@ContextConfiguration(classes = Config.class, initializers = ConfigFileApplicationContextInitializer.class)
@ContextConfiguration(initializers = ApolloPropertySourceInitializer.class)
@Slf4j
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment= WebEnvironment.RANDOM_PORT)
public class WebTests {
@Autowired
private TestRestTemplate restTemplate;
@Test
public void contextLoads() {
Assert.assertTrue(true);
}
@Test
public void test() {
ResponseEntity<String> phil = restTemplate.getForEntity("/{model}/list", String.class, "log");
Assert.assertEquals(phil.getStatusCode(), HttpStatus.OK);
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment