Jac*_*ies 5 java junit spring spring-aop aspect
我想知道在测试方面功能时我做错了什么。该方面正在生产中运行(已通过 QA 测试),但我正在尝试让我的集成单元测试通过。这是我的代码:
@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();
}
Run Code Online (Sandbox Code Playgroud)
我的 DAO 方法仅返回所有用户的列表,但是当使用方面时,它会限制显示的用户。
@Repository
UsersDaoImpl implements UsersDao {
...
}
Run Code Online (Sandbox Code Playgroud)
和我的用户服务:
@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);
}
}
Run Code Online (Sandbox Code Playgroud)
在我的单元测试中,我正在执行以下操作:
@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);
}
Run Code Online (Sandbox Code Playgroud)
我还添加了 xml 配置:
context:annotation-config></context:annotation-config>
<aop:aspectj-autoproxy proxy-target-class="true"/>
<context:component-scan base-package="com.example.aspect" />
Run Code Online (Sandbox Code Playgroud)
谁能告诉我我做错了什么?
从我对你的测试的了解来看,它应该可以工作 - 所以你需要对类路径扫描进行一些调整,确保测试使用预期的配置等。
我建议暂时添加:
@Autowired
ApplicationContext context;
@Before
public void dumpBeans() {
System.out.println(context.getBeansOfType(UsersDao.class));
}
Run Code Online (Sandbox Code Playgroud)
或者,更简单地说,System.out.println(usersDao.getClass())在测试方法中。
您还可以在调试器中运行测试 - 在测试类中添加断点,并检查usersDao运行时是什么类。
| 归档时间: |
|
| 查看次数: |
2773 次 |
| 最近记录: |