测试类小tips

一个java文件下 可以写多个class
不同于内部类的模式
public class Outer{
public class Inner{
}
}

有时候我们可以直接把测试类写在需求类的下面(在同一个java文件)这样可以省去引用的麻烦
需要注意的是 一个java文件的根目录下只能有一个public的 class class或者interface!

class Application{
}

class Test{
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
Spring Boot 提供了多种测试注解和工具类,可以方便地编写测试类。下面是一些常用的测试类示例: 1. 单元测试类 单元测试类是用来测试单个类或方法是否符合预期的,通常不需要启动 Spring 应用。 ```java import org.junit.Test; import static org.junit.Assert.assertEquals; public class MyTest { @Test public void test() { assertEquals(2, 1 + 1); } } ``` 2. 集成测试类 集成测试类是用来测试应用的各个组件是否能够协同工作,通常需要启动 Spring 应用。 ```java 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.context.junit4.SpringRunner; @RunWith(SpringRunner.class) @SpringBootTest public class MyTest { @Autowired private MyService myService; @Test public void test() { myService.doSomething(); // 测试代码 } } ``` 3. Web测试类 Web 测试类是用来测试 Web 应用的,通常需要启动 Spring 应用,并使用 Spring Boot 提供的测试工具类进行测试。 ```java import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.junit4.SpringRunner; import org.springframework.test.web.servlet.MockMvc; import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; import org.springframework.test.web.servlet.result.MockMvcResultMatchers; @RunWith(SpringRunner.class) @SpringBootTest @AutoConfigureMockMvc public class MyTest { @Autowired private MockMvc mockMvc; @Test public void test() throws Exception { mockMvc.perform(MockMvcRequestBuilders.get("/hello")) .andExpect(MockMvcResultMatchers.status().isOk()); } } ``` 在这个示例中,`@AutoConfigureMockMvc` 注解会自动配置一个 `MockMvc` 对象,用于模拟 HTTP 请求和响应。`mockMvc.perform()` 方法可以发送 HTTP 请求,`.andExpect()` 方法可以验证 HTTP 响应。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

mm1296939448

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值