发布时间:2024-04-28 09:01
1.在pom.xml中添加mockito的依赖
org.mockito
mockito-all
2.0.2-beta
2.测试类
public class Test1 {
@InjectMocks
UsersServiceImpl usersServiceImpl;
@Mock
UserMapper mapper;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
}
@Test
public void testGet() {
List list = new ArrayList();
User user = new User();
Date time = new Date();
Timestamp timestamp = new Timestamp(time.getTime());
user.setId("500151");
user.setAge(18);
user.setName("aaa");
user.setBirthday(timestamp);
user.setStats(1);
user.setOther(new BigDecimal(1));
list.add(user);
when(mapper.selectUsers()).thenReturn(list);
List relist = usersServiceImpl.selectUser();
System.out.println(relist);
}
}
3.service和service的实现类
@Service
@Transactional
public class UsersServiceImpl implements UsersService {
@Autowired
private UserMapper usermapper;
@Override
public List selectUser() {
return this.usermapper.selectUsers();
}
public interface UsersService {
List selectUser();
}
4.dao层
@Mapper
public interface UserMapper {
List selectUsers();
}