为什么我的@Before注释在Spring应用程序中不起作用?

时间:2016-05-03 08:09:58

标签: java spring junit spring-aop spring-annotations

我正在训练Spring。我的应用程序并不完美,但这不是我的问题。 我有一个Aspect,必须在Dao书中的save()方法之前执行。但是当我在调试模式下启动JUnit测试时,从未到达Aspect的before方法(checkNotNullId())的第一行,并且当我的测试到达{{1}时,我得到AssertionError } 方法。为什么会这样?这是我的JUnit类,Aspect类和类fail()指向:

BookDaoAdvisor

@Before

BookDaoImpl

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.aop.MethodBeforeAdvice;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import pl.spring.demo.annotation.NullableId;
import pl.spring.demo.common.Sequence;
import pl.spring.demo.dao.AbstractDao;
import pl.spring.demo.exception.BookNotNullIdException;
import pl.spring.demo.to.IdAware;

import java.lang.reflect.Method;

@Aspect
public class BookDaoAdvisor {

@Autowired
private Sequence sequence;
private AbstractDao<? extends IdAware> objects;
@SuppressWarnings("unchecked")
@Before("execution(* pl.spring.demo.dao.impl.BookDaoImpl.save(..))")
public void before(JoinPoint joinPoint, NullableId nullableId) throws BookNotNullIdException {
    checkNotNullId(joinPoint.getArgs()[0]);
    if (joinPoint.getThis() instanceof AbstractDao){
        objects = (AbstractDao<? extends IdAware>)joinPoint.getThis();
        setNextId(joinPoint.getArgs()[0]);
    }
}

private void checkNotNullId(Object o){
    if (o instanceof IdAware && ((IdAware) o).getId() != null) {
        throw new BookNotNullIdException();
    }
}
private void setNextId(Object o){
    if (o instanceof IdAware){
        ((IdAware) o).setId(sequence.nextValue(objects.findAll()));
    }
}

}

JUnit的

package pl.spring.demo.dao.impl;

import pl.spring.demo.annotation.NullableId;
import pl.spring.demo.common.Sequence;
import pl.spring.demo.dao.BookDao;
import pl.spring.demo.exception.BookNotNullIdException;
import pl.spring.demo.mapper.BookEntityBookToMapper;
import pl.spring.demo.to.AuthorTo;
import pl.spring.demo.to.BookEntity;
import pl.spring.demo.to.BookTo;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.aspectj.lang.annotation.Aspect;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class BookDaoImpl implements BookDao {

private final Set<BookEntity> ALL_BOOKS = new HashSet<>();
@Autowired
private Sequence sequence;
@Autowired
private BookEntityBookToMapper bookMapper;

public BookDaoImpl() {
    addTestBooks();
}

@Override
public List<BookTo> findAll() {
    List<BookTo> books = new ArrayList<BookTo>();
    for (BookEntity book: ALL_BOOKS) {
        books.add(bookMapper.mapBookEntityToBookTo(book));
    }
    return books;
}

@Override
public List<BookTo> findBookByTitle(String title) {
    method code...
}

@Override
public List<BookTo> findBooksByAuthor(String authorName) {
    method code...
}

@Override
@NullableId
public BookTo save(BookTo book) throws BookNotNullIdException {
    ALL_BOOKS.add(bookMapper.mapBookToToBookEntity(book));
    return book;
}

public void setSequence(Sequence sequence) {
    this.sequence = sequence;
}

private void addTestBooks() {
    ALL_BOOKS.add(new BookEntity(1L, "book1", "author1"));
    ALL_BOOKS.add(new BookEntity(2L, "book2", "author2"));
    ALL_BOOKS.add(new BookEntity(3L, "book3", "author3"));
    ALL_BOOKS.add(new BookEntity(4L, "book4", "author4"));
    ALL_BOOKS.add(new BookEntity(5L, "book5", "author5"));
    ALL_BOOKS.add(new BookEntity(6L, "book6", "author6"));
}
}

返回

package pl.spring.demo.service;

import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.CacheManager;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import pl.spring.demo.exception.BookNotNullIdException;
import pl.spring.demo.to.BookTo;

import java.util.List;

import static org.junit.Assert.*;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "CommonServiceTest-context.xml")
public class BookServiceImplTest {

@Autowired
private BookService bookService;
@Autowired
private CacheManager cacheManager;

@Before
public void setUp() {
    cacheManager.getCache("booksCache").clear();
}

@Test(expected = BookNotNullIdException.class)
public void testShouldThrowBookNotNullIdException() {
    // given
    final BookTo bookToSave = new BookTo();
    bookToSave.setId(22L);
    // when
    bookService.saveBook(bookToSave);
    // then
    fail("test should throw BookNotNullIdException");
}
}

bookService.saveBook()

这是BookDaoImpl类实现的接口方法。

我的错误:

bookDao.save()

XML:

java.lang.Exception: Unexpected exception, expected<pl.spring.demo.exception.BookNotNullIdException> but was<java.lang.AssertionError>
at org.junit.internal.runners.statements.ExpectException.evaluate(ExpectException.java:28)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:73)
at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:82)
at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:73)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:224)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:83)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:68)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:163)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)

Caused by: java.lang.AssertionError: test should throw BookNotNullIdException
at org.junit.Assert.fail(Assert.java:88)
at pl.spring.demo.service.BookServiceImplTest.testShouldThrowBookNotNullIdException(BookServiceImplTest.java:63)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at     org.junit.internal.runners.statements.ExpectException.evaluate(ExpectException.java:19)
... 22 more

将BookDaoAdvisor添加到XML后出现异常:

<?xml version="1.0" encoding="UTF-8"?>
<beans xsi:schemaLocation="http://www.springframework.org/schema/beans 
http://www.springframework.org/schema/beans/spring-beans.xsd                   http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd" 
xmlns:context="http://www.springframework.org/schema/context" 
xmlns:aop="http://www.springframework.org/schema/aop"     xmlns:util="http://www.springframework.org/schema/util" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans">

<import resource="cache-context.xml" />
<context:annotation-config/>
<aop:aspectj-autoproxy/>
<context:component-scan base-package="pl.spring.demo"></context:component-scan>
<util:properties location="classpath:config/application.properties" id="applicationProperties"/>
<bean id="bookService" class="pl.spring.demo.service.impl.BookServiceImpl"/>
<bean id="bookDaoAdvisor" class="pl.spring.demo.aop.BookDaoAdvisor"/>

2 个答案:

答案 0 :(得分:1)

从我的观点来看,有三件事需要检查:

  • 首先,建议:@Before("execution(* pl.spring.demo.dao.impl.BookDaoImpl.save(..))"),您的建议似乎是另一种方法(没有参数),并且缺少执行组;
  • 第二,@Aspect应该是一个spring bean,是否被添加到上下文中(在xml上声明或用@Component注释)?
  • 第三,从NullableId nullableId课程的@Before方法中移除@Aspect

答案 1 :(得分:0)

你需要改变BookDaoAdvisor.java中的@Before

@Before("execution(* pl.spring.demo.dao.impl.BookDaoImpl.save(..))")

在此更改后,您的BookDaoAdvisor.java将

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.aop.MethodBeforeAdvice;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import pl.spring.demo.annotation.NullableId;
import pl.spring.demo.common.Sequence;
import pl.spring.demo.dao.AbstractDao;
import pl.spring.demo.exception.BookNotNullIdException;
import pl.spring.demo.to.IdAware;

import java.lang.reflect.Method;

@Aspect
public class BookDaoAdvisor {

@Autowired
private Sequence sequence;
private AbstractDao<? extends IdAware> objects;
@SuppressWarnings("unchecked")
@Before("execution(* pl.spring.demo.dao.impl.BookDaoImpl.save(..))")
public void before(JoinPoint joinPoint, NullableId nullableId) throws BookNotNullIdException {
    checkNotNullId(joinPoint.getArgs()[0]);
    if (joinPoint.getThis() instanceof AbstractDao){
        objects = (AbstractDao<? extends IdAware>)joinPoint.getThis();
        setNextId(joinPoint.getArgs()[0]);
    }
}

private void checkNotNullId(Object o){
    if (o instanceof IdAware && ((IdAware) o).getId() != null) {
        throw new BookNotNullIdException();
    }
}
private void setNextId(Object o){
    if (o instanceof IdAware){
        ((IdAware) o).setId(sequence.nextValue(objects.findAll()));
    }
}

}