pom文件
依赖
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
打包时去掉测试包
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.18.1</version>
<configuration>
<skipTests>true</skipTests>
</configuration>
</plugin>
</plugins>
java代码
@RunWith(SpringRunner.class)
@SpringBootTest
public class OfficeServiceImplTest {
@Resource
private OfficeService officeService;
@Transactional
@Rollback(true)// 事务自动回滚,默认是true。可以不写
@Test
public void findOfficeTreeOneByOne2() {
}
}
web(controller)调用
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class OfficeControllerTest {
@Resource
private OfficeController officeController;
@Autowired
private TestRestTemplate restTemplate;
private final static String AUTHORIZATION = "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1c2VybmFtZSI6ImFkbWluIiwicGFzc3dvcmQiOiIyNjU3ODY4NmU0Nzg5NzZlYTI3N2RiYWRmNmRkZGRlZiIsImV4cCI6MTYyOTE4NTM0NSwibmJmIjoxNjI5MDk4OTQ1fQ.WdI5jIycD1wUuWEWwUq1crwfDnL0Hzsf980LtGD87g0";
@Test
public void findOfficeTreeOneByOne2() {
MultiValueMap<String, String> map= new LinkedMultiValueMap<String, String>();
map.add("activityId", "E08AB729AE8A51069765F0933C4E0440" );
map.add("activityGroupId", "E08AB729AE8A51069765F0933C4E0440" );
map.add("flag", "0" );
map.add("isNeedCurDeptCode", "true" );
HttpHeaders headers = new HttpHeaders();
MediaType type = MediaType.parseMediaType("application/json; charset=UTF-8");
headers.setContentType(type);
headers.put("Accept", Arrays.asList(MediaType.APPLICATION_JSON_VALUE,MediaType.TEXT_PLAIN_VALUE,MediaType.ALL_VALUE));
headers.add("Authorization",AUTHORIZATION);
HttpEntity<MultiValueMap<String, String>> request = new HttpEntity<>(map, headers);
ResponseEntity<Result> resultResponseEntity = restTemplate.postForEntity("/office/findOfficeTreeOneByOne2",
request, Result.class);
System.out.println();
}
}
该博客介绍了如何在SpringBoot项目中配置pom.xml以排除测试依赖,并在打包时跳过测试。同时展示了Java代码中使用SpringRunner和@SpringBootTest进行服务和控制器测试的方法,包括HTTP请求的模拟和响应处理。
918

被折叠的 条评论
为什么被折叠?



