当前位置: 首页 > wzjs >正文

java开发网站如何做哪些网站可以免费申请域名

java开发网站如何做,哪些网站可以免费申请域名,什么是wap网站,为什么做pc网站文章目录 引言标准用法必需依赖项核心注解说明代码示例 当涉及静态方法时的测试策略必需依赖项核心注解说明代码示例 引言 之前在编写 Controller 层的单元测试时,我一直使用 SpringBootTest 注解,但它会加载整个 Spring 应用上下文,资源开销…

文章目录

      • 引言
      • 标准用法
        • 必需依赖项
        • 核心注解说明
        • 代码示例
      • 当涉及静态方法时的测试策略
        • 必需依赖项
        • 核心注解说明
        • 代码示例

引言

之前在编写 Controller 层的单元测试时,我一直使用 @SpringBootTest 注解,但它会加载整个 Spring 应用上下文,资源开销大,我更倾向于采用更轻量级的测试策略,专注于 Web 层的功能验证。

在最近的单元测试撰写中,我采用了新的方法,并在此归纳记录:

标准用法

必需依赖项

<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><!-- 版本可根据项目实际情况调整,需要和spring-boot版本适应--><scope>test</scope>
</dependency>
<!-- 如果不报错,一般不需要引入 -->
<dependency><groupId>org.junit.platform</groupId><artifactId>junit-platform-launcher</artifactId><version>1.8.2</version><scope>test</scope>
</dependency>
<!-- 仅在需兼容 JUnit 4/3 时使用 -->
<dependency><groupId>org.junit.vintage</groupId><artifactId>junit-vintage-engine</artifactId><version>5.8.2</version><scope>test</scope>
</dependency>
核心注解说明
  • @WebMvcTest(controllers = YourController.class)

Spring Boot 提供的切片测试注解,专门用于测试 MVC 控制器层。它只加载 Spring MVC 相关组件(如 @Controller、@RestController、@ControllerAdvice 等),不会加载 Service、Repository 等其他 Bean。

  • @ContextConfiguration(classes = YourController.class)

明确指定测试所需加载的配置类或组件,避免因自动注入无关 Bean 而导致测试失败。

代码示例
@WebMvcTest(controllers = YourController.class)
@ContextConfiguration(classes = YourController.class)
class YourControllerTest {@Autowiredprivate MockMvc mockMvc;@MockBeanprivate YourService yourService;@Autowiredprivate ObjectMapper objectMapper;@Testvoid getSomething() throws Exception {// 模拟service层方法调用when(yourService.getSomething(anyString())).thenReturn(R.ok());// 模拟请求参数HashMap<String, Object> requestBody = new HashMap<>();requestBody.put("testParam", "test");// 开始模拟mockMvc.perform(MockMvcRequestBuilders.post("/xx/getSomething").contentType(MediaType.APPLICATION_JSON).content(objectMapper.writeValueAsString(requestBody))).andDo(MockMvcResultHandlers.print()).andExpect(MockMvcResultMatchers.status().isOk()).andExpect(jsonPath("$.code").value(R.ok().getCode())).andExpect(jsonPath("$.data").isEmpty());}
}

当涉及静态方法时的测试策略

在极端情况下,如果 Controller 中存在通过工厂类静态方法构造的依赖(而不是通过 Spring 注入),该如何进行测试?

必需依赖项

<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><!-- 版本自选,需要和spring-boot版本适应--><exclusions><exclusion><groupId>org.mockito</groupId><artifactId>mockito-core</artifactId></exclusion></exclusions><scope>test</scope>
</dependency><!--不报错就没必要加它-->
<dependency><groupId>org.junit.platform</groupId><artifactId>junit-platform-launcher</artifactId><version>1.8.2</version><scope>test</scope>
</dependency>
<dependency><groupId>org.junit.vintage</groupId><artifactId>junit-vintage-engine</artifactId><version>5.8.2</version><scope>test</scope>
</dependency>
<!--旧版本 mockito-core 不支持静态方法 mock-->
<dependency><groupId>org.mockito</groupId><artifactId>mockito-core</artifactId><version>5.2.0</version><scope>test</scope>
</dependency>
核心注解说明
  • @ExtendWith(MockitoExtension.class)

启用 Mockito 的注解功能,包括 @Mock、@InjectMocks、@Spy 等支持。

代码示例
@ExtendWith(MockitoExtension.class)
class YourControllerTest {private ObjectMapper objectMapper = new ObjectMapper();@Testvoid getSomething() throws Exception {try(MockedStatic<XxxFactory> mocked = mockStatic(XxxFactory.class)){//用来替换的对象YourInstance instance = mock(YourInstance.class);//静态替换mocked.when(XxxFactory::getYourInstance).thenReturn(instance);//模拟方法调用when(instance.getSomething(anyString())).thenReturn(R.ok());// 模拟请求参数HashMap<String, Object> requestBody = new HashMap<>();requestBody.put("testParam", "test");// 开始模拟mockMvc.perform(MockMvcRequestBuilders.post("/xx/getSomething").contentType(MediaType.APPLICATION_JSON).content(objectMapper.writeValueAsString(requestBody))).andDo(MockMvcResultHandlers.print()).andExpect(MockMvcResultMatchers.status().isOk()).andExpect(jsonPath("$.code").value(R.ok().getCode())).andExpect(jsonPath("$.data").isEmpty());}}}
http://www.dtcms.com/wzjs/356540.html

相关文章:

  • 自己做网站怎么赚钱百度服务中心官网
  • 苏州seo网站诊断发外链的论坛
  • 如何注册公司名称优化关键词方法
  • 毕设做的网站可以用模板改吗免费自助建站哪个最好
  • 优秀的移动端网站seo综合查询怎么用的
  • wordpress 备份还原临沂seo推广外包
  • 黄山工程建设信息网站广州专业seo公司
  • 独立商城系统网站建设长沙优化网站
  • 响应式网站开发实例百度搜索排名规则
  • 成都有做网站劫持的吗宁波seo网络推广
  • 厦门网站建设慕枫项目推广计划书
  • 渠道网络科技有限公司如何优化关键词的排名
  • 营业执照上有以上除网站制作2345网址导航安装
  • wordpress网站变灰手机seo排名
  • 做网站的销售员电话话术淮北seo
  • 房子装修设计图片大全seo诊断优化专家
  • 如何自建设网站常用的关键词优化策略有哪些
  • 中山做企业网站杭州网站免费制作
  • 做外贸怎么网站找客户合肥seo优化公司
  • 物联网设计大赛官网搜索引擎优化网站排名
  • 网站建设的技术指标现在做网络推广好做吗
  • 自己建立网站怎么搞橘子seo查询
  • 如何做网站词库品牌营销推广方案怎么做
  • wordpress禁止中国ip关键词优化价格
  • 保定建设网站公司在百度怎么免费发布广告
  • 微信公众号做微网站企业邮箱查询
  • 商丘做网站百度关键词优化查询
  • 一个网站源码值多少钱网络推广平台收费不便宜
  • 做网站和做小程序有什么不同全球搜官网
  • 网站开发fsdpjq搜索引擎优化