写一个测试类测试springboot中 impl 的方法
时间: 2023-11-21 08:17:17 浏览: 60
好的,这是一个测试类的示例代码:
```java
@RunWith(SpringRunner.class)
@SpringBootTest
public class MyServiceImplTest {
@Autowired
private MyService myService;
@Test
public void testAdd() {
int a = 2;
int b = 3;
int result = myService.add(a, b);
assertEquals(5, result);
}
@Test
public void testMultiply() {
int a = 2;
int b = 3;
int result = myService.multiply(a, b);
assertEquals(6, result);
}
}
```
其中,`MyService` 是一个接口,`MyServiceImpl` 是其实现类,`add()` 和 `multiply()` 是 `MyService` 接口中的两个方法。通过 `@Autowired` 注解,我们将 `MyServiceImpl` 注入到测试类中,然后针对 `add()` 和 `multiply()` 两个方法编写测试用例,通过 `assertEquals()` 断言来验证方法返回的结果是否符合预期。