测试数据是否将存储到数据库中

时间:2016-10-28 13:13:16

标签: java spring testing

所以我有类ExceptionAspect,确保抛出的所有异常都是MyExceptions。应用程序使用Spring。

问题是如何编写测试答案:

handleException()方法中调用的registerMyException()方法是否可以将异常数据保存到DB中?

你可能会推荐mockito和junit - 如果有人可以逐步解释样本测试中发生的事情,那就太好了。

如果需要更多信息,请告知我们。 我很抱歉没有立即回答 - 我必须先收集好想法。

public abstract class ExceptionAspect {

    public void handleException(Throwable t) {
        if (t instanceof MyException) {
            MyException myException = (MyException) t;
            registerMyException(myException);
            throw myException;
        } else {
            MyException myException = new MyException(t);
            registerMyException(myException);
            throw myException;
        }
    }

    private void registerMyException(MyException ce){
        MyError myError = new MyError();
        myError.setCategory(ce.getCategory());
        myError.setType(ce.getType);
    }

   /**
    *  other methods
    */
}

此处的持久性类MyError

@Entity
@Table(name="MyError")

public class MyError implements java.io.Serializable{

private static final long serivalVersionUID = 1L;

    @Id
    @GeneratedValue(generator = "uuid2") @GenericGenerator(name = "uuid2", strategy = "uuid2")
    @Column(name="ID", unique=true, nullable=false, length=50)
    private String id;

    @Column(name="CATEGORY", nullable=true, length=50)
    private String errCategory;

    @Column(name="TYPE", nullable=true, length=50)
    private String errType;

/**
*   getter and setter here
*/
}

并且测试同时我写了 - 帖子之间的限制为90分钟刺激了头脑:)所以现在问题是如何测试它?应该检查什么 - 任何命题?

public class myExceptionTest{
    @Mock
    ExceptionAspect exceptionAspect;

    @Rule
    public ExpectedException thrown = ExpectedException.none();

    @Test
    public void throwsMyException(){
        // just to check if it works :) so it works
        thrown.expect(MyException.class);
        Throwable testThrowable = new RuntimeException();
        throw new MyException(testThrowable);
    }

    @Test
    public void doesNotThrowsMyException(){
        // just to check if it fail :) and as I expected it failed
        thrown.expect(MyException.class);
        throw new ArithmeticException();
    }
}

0 个答案:

没有答案