So I have class ExceptionAspect that ensure that all exceptions thrown are always MyExceptions. Application uses Spring.
The question is how to write tests answering:
Does registerMyException() method called in handleException() method for sure saves exceptions data into DB?
Probably You recommend mockito and junit - it would be wonderful if somebody can explain on example step by step what's going on in sample test(s).
If some more information are needed let me know. I'm sorry for not answer immediately - I have to gather thoughts before.
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
*/
}
Here's persistence class 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
*/
}
And test that meanwhile I wrote - limit of 90min between posts stimulates mind :) So now question is how to test it? What should be checked - any propositions?
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();
}
}
Aucun commentaire:
Enregistrer un commentaire