By using JMockit @Capturing, it was unable to capture call to any spring data jpa repository methods.
public interface UserRepository extends JpaRepository<UserEntity, Long> {
UserEntity findByName(String name);
}
public class DefaultUserService implements UserService {
public User getUser(Long id) {
return userRepo.findOne( id );
}
public User getUser(String name) {
return userRepo.findByName( name );
}
}
public class UserServiceTest extends AbstractServiceTest {
@Inject UserService userService;
**@Capturing UserRepository userRepo;**
@Test
public void getUser_ShouldReturnUserObject() {
**new Expectations() {{
userRepo.findByName(anyString);
result = new UserEntity(1l, null, null, null, null);
}};**
User user = userService.getUser("abc");
assertNotNull(user.getId());
assertEquals(1l, user.getId().longValue());
}
}
However by replacing
UserRepository
with
JpaRepository<UserEntity, Long>
in the test class, JMockit able to intercept call to any methods available in JpaRepository interface like findOne() or findAll().
But it just not able to capture calls to custom repository methods that extends JpaRepository such as findByName().
I prefer to use @Capturing for this scenario even though JMockit state-based testing such as MockUp and Deencapsulation can solve this issue because it is much more simpler.
Any one has any ideas to solve this issue?