2017-04-11 10 views
3

私のアスペクト機能をテストするときに何が間違っているのか不思議でした。アスペクトは生産(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" /> 

誰も私が間違っていることをアドバイスできますか?

+0

あなたのテストクラスで 'usersDao'は宣言/初期化されていますか? – slim

+0

...あなたのテストの失敗はどうですか? 'Assert'とは何か(' org.junit.Assert'は 'notNull()'メソッドを持っていません) – slim

+0

@slimお詫び申し上げます。私は 'usersDao'の宣言を追加したクラスを編集しました。 'org.springframework.util.Assert'は' notNull() 'メソッドを持っています –

答えて

0

私はあなたのテストで見ることができるものから、それは動作するはず - あなたがテストを確保し、クラスパスのスキャンについて行うにはいくつかの調整を持っている期待されるコンフィギュレーションを使用しているなど

私は一時的に追加することをお勧めします:

@Autowired 
ApplicationContext context; 

@Before 
public void dumpBeans() { 
     System.out.println(context.getBeansOfType(UsersDao.class)); 
} 

または、より簡単には、試験方法においてSystem.out.println(usersDao.getClass())

デバッガでテストを実行することもできます。テストクラスにブレークポイントを追加し、実行時にどのクラスがusersDaoであるかを確認することができます。

+0

こんにちは@slim、上記のサービスクラス(私たちのアプリケーションで他の統合テストはこれらを使用する)を使用するためにテストで上記の私のコードを編集したので、私はそれが何か変わっても私はそれを得ることができませんでしたワーキング。 –

関連する問題