私のアスペクト機能をテストするときに何が間違っているのか不思議でした。アスペクトは生産(QAによるテスト合格)で作業していますが、合格単位テストを合格させようとしています。ここに私のコードは次のとおりです。Springのアスペクト機能の統合テストを書く
@Aspect
@Component
public class MyAspect {
@Pointcut("execution(* com.example.dao.UsersDao(..)) && args(.., restrictions)")
protected void allUsersPointcut(List<String> restrictions) {
}
@Around("allUsersPointcut(restrictions)")
public Object applyUserRestrictions(final ProceedingJoinPoint pjp, List<String> restrictions) throws Throwable {
String restrict = "Jack";
restrictions.add(restrict);
return pjp.proceed();
}
マイDAOメソッドは、単に、すべてのユーザーのリストを返しますが、側面を使用する場合には、ユーザーが表示されているものを制限します。
@Repository
UsersDaoImpl implements UsersDao {
...
}
そして、私のUsersService:私のユニットテストで
@Service
public class UsersService implements UsersService {
@Autowired
protected UsersDAO usersDAO;
...
@Transactional(readOnly=true)
public List<String> findUsers(List<String> restrictions) {
return this.usersDAO.findUsers(restrictions);
}
}
私は次のことをやっている:
@RunWith(SpringJUnit4ClassRunner.class)
public class UserTest {
@Autowired
UsersService usersService;
@Test
public void testAspect() {
List<String> restrictions = null;
List<String> users = this.usersService.findUsers(restrictions);
Assert.notNull(users);
}
私は、XML confgurationをも追加しました:
context:annotation-config></context:annotation-config>
<aop:aspectj-autoproxy proxy-target-class="true"/>
<context:component-scan base-package="com.example.aspect" />
誰も私が間違っていることをアドバイスできますか?
あなたのテストクラスで 'usersDao'は宣言/初期化されていますか? – slim
...あなたのテストの失敗はどうですか? 'Assert'とは何か(' org.junit.Assert'は 'notNull()'メソッドを持っていません) – slim
@slimお詫び申し上げます。私は 'usersDao'の宣言を追加したクラスを編集しました。 'org.springframework.util.Assert'は' notNull() 'メソッドを持っています –