我正在尝试为Spring启动项目实现单元测试。
我是Java单元测试的新手,我知道我可以在服务层测试我的项目。这里是:
@Mock
private ICampusRepository campusRepository;
@InjectMocks
private CampusServiceImplementation campusService;
private List<Campus> campusList = new ArrayList<>();
@BeforeEach
void setUp() {
MockitoAnnotations.openMocks(this);
campusList.add(new Campus(1, "Campus Name 1", "ACTIVE"));
campusList.add(new Campus(2, "Campus Name 2", "ACTIVE"));
campusList.add(new Campus(3, "Campus Name 3", "INACTIVE"));
}
然后,对于测试,我有一些非常基本的测试:
@Test
void getAllCampus() {
when(campusRepository.findAll()).thenReturn(campusList);
assertNotNull(campusService.getAllCampus());
}
我想做一个测试来验证某个方法是否返回带有"ACTIVE"的校园列表。属性,我试着这样做:
@Test
void listAllActiveCampus() {
when(campusRepository.findAllActiveCampus()).thenReturn(campusList);
assertEquals(2, campusService.listAllActiveCampus().size());
}
但测试结果是:
org.opentest4j.AssertionFailedError:
Expected :2
Actual :0
问题是:我应该创建一个只有2个值的新数组吗?有没有更好的方法来创建更好的结论性测试?
编辑
campusService代码:
@Override
public List<Campus> getAllCampus() {
return campusRepository.findAll();
}
...
@Override
public List<Campus> listAllActiveCampus() {
return campusRepository.findAllActiveCampus();
}
存储库:public interface ICampusRepository extends JpaRepository<Campus, Integer> {
@Query(value = "SELECT * FROM catSede WHERE sedeEstatusId = 3", nativeQuery = true)
List<Campus> findAllActiveCampus();
}
编辑2
完整测试类:
@SpringBootTest
class CampusServiceImplementationTest {
@Mock
private ICampusRepository campusRepository;
@InjectMocks
private CampusServiceImplementation campusService;
@BeforeEach
void setUp() {
MockitoAnnotations.openMocks(this);
}
@Test
void getAllCampus() {
List<Campus> campusList = new ArrayList<>();
campusList.add(new Campus(1, "Campus Name 1", "ACTIVE"));
campusList.add(new Campus(2, "Campus Name 2", "ACTIVE"));
campusList.add(new Campus(3, "Campus Name 3", "INACTIVE"));
when(campusRepository.findAll()).thenReturn(campusList);
List<Campus> result = campusRepository.findAll();
assertNotNull(result);
assertEquals(3, result.size());
verify(campusRepository, times(1)).findAll();
}
@Test
void listAllActiveCampus() {
List<Campus> campusList = new ArrayList<>();
campusList.add(new Campus(1, "Campus Name 1", "ACTIVE"));
campusList.add(new Campus(2, "Campus Name 2", "ACTIVE"));
campusList.add(new Campus(3, "Campus Name 3", "INACTIVE"));
when(campusRepository.findAllActiveCampus()).thenReturn(campusList);
List<Campus> result = campusRepository.findAllActiveCampus();
assertNotNull(result);
assertEquals(2, result.size());
verify(campusRepository, times(1)).findAllActiveCampus();
}
}
服务类:
@Service
@RequiredArgsConstructor
public class CampusServiceImplementation implements ICampusService {
private final ICampusRepository campusRepository;
@Override
public List<Campus> getAllCampus() {
return campusRepository.findAll();
}
@Override
public List<Campus> listAllActiveCampus() {
return campusRepository.findAllActiveCampus();
}
}
存储库:
public interface ICampusRepository extends JpaRepository<Campus, Integer> {
@Query(value = "SELECT * FROM catSede WHERE sedeEstatusId = 3", nativeQuery = true)
List<Campus> findAllActiveCampus();
}
当你写
when(campusRepository.findAllActiveCampus()).thenReturn(campusList);
assertEquals(2, campusService.listAllActiveCampus().size());
和您的setUp()
如下所示,那么您应该得到错误:
org.opentest4j.AssertionFailedError:
Expected :2
Actual :3
,因为你配置了你的' campusRepository ',所以当调用' findAllActiveCampus() '时,它应该返回' campusList ',其中又包含32不是2元素都是"ACTIVE"
请记住,在这个测试中根本没有执行@Query(value = "SELECT * FROM catSede WHERE sedeEstatusId = 3", nativeQuery = true)
,因为存储库只是一个模拟!