I am writing a test for already built java class function. I am writing tests using Testng and Mockito and have a Data Provider.
This is my Test
#Test(dataProvider = "myProvider", dataProviderClass = StaticDataProvider.class,
expectedExceptions = SomeException.class)
public void myControllerTest(String argument) throws Exception {
// Mocked object bussiness\
Boolean resultantObject = business.getList(argument);
Assert.assertTrue(resultantObject);
}
This is my Controller which I want to test
public Boolean controller(String argument) {
if(argument != null) {
throw new someException();
} else {
System.out.println("Sucess");
return true;
}
}
This is my Data Providor
#DataProvider(name = "myProvider")
public static Object[][] getDirectoryList() throws Exception {
Object[][] result = null;
// case1 throws SomeException
String testData1 = null;
// case2 don't throw exception
String testData2 = "String";
result = new Object[][] { { testData1 }, { testData2 } };
return result;
}
The problem here I am facing is, I don't want to create another test just to test both buggy and non buggy code and complete my test coverage using a single test case. But when I put Expected Exception on top, it fails on correct code, and when I dont, it fails on buggy code.
NOTE: This is example code and may not work, this is just to take an idea of scenario I am working on and what I am expecting.
Even if you ignore the "one test, one assertion" purist perspective, I think most people agree you should split tests that involve error conditions from tests that prove normal behaviour.
If you want to test multiple error conditions within one test (or if you're really keen on continuing with your plan), you can use this pattern:
try {
// something that should cause an exception
fail("Exception expected");
} catch (ExactlyTheRightException e) {
// ignored
}
Related
I am new in Unit Testing and I have sometimes such situations with multiple conditions. However, I am not sure if I re-mock or verify the same cases for each test.
For example, I am trying to write Unit Tests for the following service method:
public void create(Request request) {
// code omitted
if (!employeeService.existsByUuid(uuid)) {
throw new EntityNotFoundException("Not found");
}
EmployeeDTO employee = employeeService.save(...);
if (!departmentService.existsByUuid(employee.getDepartment())) {
throw new EntityNotFoundException("Not found");
}
}
I think I need to write my tests for the following scenarios:
1. when employeeService.existsByUuid(uuid) == false, then throw new EntityNotFoundException. then verify employeeService.save() and departmentService.existsByUuid() is never called.
2. when employeeService.existsByUuid(uuid) == true then employeeService.save() is called and I assert the values. and then verify employeeService.save() and departmentService.existsByUuid() is never called.
3. when departmentService.existsByUuid() == false then throw new EntityNotFoundException. At this stage, I also mock employeeService.existsByUuid(uuid) as true so that test passes the first condition. However, I am not sure if do I need to assert the second part; employeeService.save() is called and I assert the values. Do I assert of the returned values or just verify that method is called 1 time. Because I already asserted its value and the 3rd test is just for the 3rd condition.
Any idea for this kind of scenarios when we have multiple condition and may need to re-test the same condition again and again?
You should not try to test your code line by line, but with cases that cover a single meaningful scenario. So if you already have a case which checks a condition, you don't have to repeat those asserts in other test cases.
In your example I think these could be the core cases:
if the UUID does not exist, an exception is thrown and the employee is not saved
if the UUID exists, all the employee fields are saved correctly
if the employee is saved, but the employee's department does not exist an exception is thrown
To test them you could do something like this:
EmployeeService employeeService = mock(EmployeeService.class);
case 1:
when(employeeService.existsByUuid(employeeUuid)).thenReturn(false);
try {
testObject.create(request);
fail();
}
catch(EntityNotFoundException e) {
verify(employeeService, never()).save(...);
}
case 2:
when(employeeService.existsByUuid(employeeUuid)).thenReturn(true);
when(employeeService.existsByUuid(departmentUuid)).thenReturn(true);
testObject.create(request);
verify(employeeService).save(field1, field2, ...);
case 3:
when(employeeService.existsByUuid(employeeUuid)).thenReturn(true);
when(employeeService.existsByUuid(departmentUuid)).thenReturn(false);
try {
testObject.create(request);
fail();
}
catch(EntityNotFoundException e) {
// success
}
BTW you can also indicate expected exceptions in the #Test annotation, but then you cannot do any further checking on the results:
#Test(expected = EntityNotFoundException.class)
public void test3() {
when(employeeService.existsByUuid(employeeUuid)).thenReturn(true);
when(employeeService.existsByUuid(departmentUuid)).thenReturn(false);
testObject.create(request);
}
You can use mockito verify and assert throws to test your objectives something like below
#Test
public void testOne(){
when(employeeService.existsByUuid(uuid)).thenReturn(false);
assertThrows(EntityNotFoundException.class, () -> {
create(request);
});
verify(employeeService, times(0)).save(eq(empObj));
verify(departmentService, times(0)).existsByUuid(eq(departmentObj));
}
#Test
public void testTwo(){
when(employeeService.existsByUuid(uuid)).thenReturn(true);
when(departmentService.existsByUuid(uuid)).thenReturn(true);
create(request);
verify(employeeService, times(1)).save(eq(empObj));
verify(departmentService, times(1)).existsByUuid(eq(departmentObj));
}
#Test
public void testThree(){
when(employeeService.existsByUuid(uuid)).thenReturn(true);
when(departmentService.existsByUuid(uuid)).thenReturn(false);
assertThrows(EntityNotFoundException.class, () -> {
create(request);
});
verify(employeeService, times(1)).save(eq(empObj));
verify(departmentService, times(1)).existsByUuid(eq(departmentObj));
}
For regression testing (not unit testing), where we have elaborate scenarios written in TestNG, is there a proper place the Assert checks should be done? Does it matter or not if it's in the test case, or in a calling method? For example:
This test case calls a validation method that contains the asserts:
#Test
public void test1() {
validateResponse();
}
public void validateResponse() {
Assert.assertEquals(a, "123");
Assert.assertEquals(b, "455");
Assert.assertEquals(c, "5678");
Assert.assertEquals(d, "3333");
}
This test case asserts based on the return value of the verification method:
#Test
public void test1() {
Assert.assertTrue(validateResponse());
}
public boolean void validateResponse() throws Exception {
try {
if (!a.equals("123")) throw new Exception();
if (!b.equals("455")) throw new Exception();
if (!c.equals("5678")) throw new Exception();
if (!d.equals("3333")) throw new Exception();
} catch (Exception e) {
e.printStackTrace();
return false;
}
return true;
}
Your assert should be as specific and granular as possible to help the developer quickly identify the problem. e.g.
#Test
public void testResponseFields(){
// create response to be tested
// JUnit style
Assert.assertEquals("Response 'alpha' should be '123'", 123, response.getAlpha());
// TestNG style
Assert.assertEquals(response.getAlpha(), 123, "Response 'alpha' should be '123'");
}
Once you set a failure message in the Assert.assertXX call, it becomes more of a moot point as to where the Assert is called as you will have a message explaining the problem and a stack trace to see where and when it failed.
I am trying to execute a data driven test using TestNG (& of course the dataprovider annotation).
My scenario is something like this ...
Use dataProvider to have a 2 dim array. (I am using this to read from Excel, but avoided it for brevity of the question).
#DataProvider(name = "featureTest")
public Object[][] dataSets() throws Exception {
return new Object[][] { {"TC_01", "testuser_1", "Test#123", "ABC Street", "123-456-7899" },
{ "TC_02", "testuser_1", "Test#123", "PQR Street", "222-456-7899" }
};
}
In the #Test method, there are several methods as per the functional flow -
#Test(dataProvider = "featureTest")
public void executeTest(String... data) throws Exception {
try{
feature_1.execute(data);
feature_2.execute(data);
feature_3.execute(data);
feature_4.execute(data);
}
catch(Exception e){
log.error("Error has occured");
}
}
Now my main problem is that the functional error can occur anywhere out of these 4 (n) methods that I specify in my #Test.
In case of an exception in any of the methods, I need to "Skip" the particular dataset and proceed to the next one.
For eg: In during execution of TC_01, an exception occured in feature_2.execute(), it should not execute the feature_3 and feature_4 methods.
Note:
I tried handling it using #BeforeMethod, #AfterMethod but still it goes through the unwanted methods that I want to avoid.
Thanks in advance for your help/inputs && apologies for the long question although a relatively simple concept to explain !!!
Its very simple. Use return!
Use a condition or try catch to evaluate the failure and then use a return statement;
One approach I can think of is the factory approach,
Your testclass
class Test{
Data data;
Test(Data){
this.data=data;
}
#Test
test1(){
feature_1.execute(data);
}
#Test
test2(dependsOnMethods ="test1"){
feature_2.execute(data);
}
#Test(dependsOnMethods ="test2")
test3(){
feature_3.execute(data);
}
#Test(dependsOnMethods ="test3")
test4(){
feature_4.execute(data);
}
}
And in your factory class
class Factory{
#Factory(DataProvider = "myDP")
public Object[] factoryTest(Data data){
new Test(data);
}
#DataProvider
public Object [][] myDP(){
enter code here
}
}
I am trying to test file manipulation with my APP. First of all I wanna check that whenever I call a function that reads the file, this function will throw an Exception because the file isn't there.
However, I don't seem to understand how to achieve this... This is the code I designed, but it doesn't run ... the normal JUNIT says the FILEPATH wasn't found, the android JUNIT says, the Test could not be run.
The folder: /data/data/example.triage/files/ is already available in the virtual device...
#Before
public void setUp() throws Exception {
dr = new DataReader();
dw = new DataWriter();
DefaultValues.file_path_folder = "/data/data/example.triage/files/";
}
#After
public void tearDown() throws Exception {
dr = null;
dw = null;
// Remove the patients file we may create in a test.
dr.removeFile(DefaultValues.patients_file_path);
}
#Test
public void readHealthCardsNonExistentPatientsFile() {
try {
List<String> healthcards = dr.getHealthCardsofPatients();
fail("The method didn't generate an Exception when the file wasn't found.");
} catch (Exception e) {
assertTrue(e.getClass().equals(FileNotFoundException.class));
}
}
It doesn't look like you are checking for the exception in a way that correlates with the JUnit API.
Have you tried to make the call:
#Test (expected = Exception.class)
public void tearDown() {
// code that throws an exception
}
I don't think you want the setup() function to be able to generate an exception, since it is called before all other test cases.
Here's another way to test exceptions:
Exception occurred = null;
try
{
// Some action that is intended to produce an exception
}
catch (Exception exception)
{
occurred = exception;
}
assertNotNull(occurred);
assertTrue(occurred instanceof /* desired exception type */);
assertEquals(/* expected message */, occurred.getMessage());
So I would make you setup() code not throw an exception and move the exception generating code to a test method, using an appropriate way to test for it.
I have a java program which throws an exception with 2 different messages for 2 different scenarios and I want the Junit test case to check for equality for both of these messages. As an example -
public void amethod() {
// do some processing
if(scenario1 == true) {
throw new MySystemException("An error occured due to case 1 being incorrect.");
}
else if(scenario2 == true) {
throw new MySystemException("An error occured as case 2 could not be found");
}
}
Now the JUnit for this would be something like-
public void testAMethod() {
// do something
assertEquals("Expected", "Actual");
}
As I understand, in this above example, if I use the Scenario1 exception message the junit will fail when an exception is thrown for Scenario2 and vice versa.
I would like to know if there is any other way provided in Junit by which I can use this one test method and check for both the messages for the test to pass?
Something like an OR, if possible to provide the "Expected" value with both these expected message.
I hope my query is clear enough.
Thanks
UPDATE
Sorry for the delayed response, had got caught up with some other urgent matter.
Thank you all for the very nice suggestions, it certainly has helped me to understand a bit better now.
Eventually, to keep it rather simple I decided to implement a somewhat similar solution suggested by Don Roby. So created a new test class which looks like -
public void testAMethodScenario1() {
// do the necessary
assertEquals("Expected Exception Message 1", "Actual");
}
public void testAMethodScenario2() {
// do the necessary
assertEquals("Expected Exception Message 2", "Actual");
}
Thank you all again for your responses.
I think you need to manually catch the exception (for each scenario) and individually check the message:
try {
// trigger scenario 1
fail("An exception should have been thrown here !");
} catch (MySystemException e1) {
assertEquals("Wrong error message", m1, e1.getMessage());
}
try {
// trigger scenario 2
fail("An exception should have been thrown here !");
} catch (MySystemException e2) {
assertEquals("Wrong error message", m2, e2.getMessage());
}
Of course, you can have these scenarios defined as enum constants and simply iterate through them and check each of them within a loop, since the "copy/paste design pattern" is pretty obvious in the above code. :)
You seem to be asking two things here, how to test an exception and how to assert that a value matches either of two possible expected values.
To test for an exception, you can either use a JUnit4 annotation:
#Test(expected=MySystemException.class)
public void testException() {
amethod();
}
or use a try-catch in your test:
#Test
public void testException() {
try {
amethod();
fail("MySystemException expected");
}
catch (MySystemException e) {
// Success!
}
}
And if you have only one message, in the try-catch version you can assert that you got it with an AssertEquals in the catch block.
The best testing would have separate tests for your two scenarios, and expect the correct single message. Better code might in fact have distinct exceptions for the two situations.
But the need for a more complex assertion than simple equality does come up anyway, and there's an elegant solution for it in Hamcrest matchers.
Using that for this situation, you could write something like (untested - don't trust my syntax completely):
#Test
public void testException() {
try {
amethod();
fail("MySystemException expected");
}
catch (MySystemException e) {
String expectedMessage1 = "An error occured due to case 1 being incorrect.";
String expectedMessage2 = "An error occured as case 2 could not be found";
assertThat(e.getMessage(),
anyOf(equalTo(expectedMessage1), equalTo(expectedMessage2)));
}
}
Can you predict which scenario will occur? If so, Costi's answer is correct. If not, because there's some randomness or whatever, you can write:
#Test
public void testAmethodThrowsException() {
try {
amethod();
fail("amethod() should have thrown an exception");
}
catch (MySystemException e) {
String msg = e.getMessage();
assertTrue("bad message: " + msg, msg.equals("An error occured due to case 1 being incorrect.") || msg.equals("An error occured as case 2 could not be found"));
}
}
The declared types of exception thrown bya method are part of its API. If you really want to distinguish different failure modes, you should declare a different exception type for each failure mode.
So, something like this:
/**
* Do something.
* #throws MySystemException1 in case 1.
* #throws MySystemException2 if Foo not found.
*/
public void amethod() {
// do some processing
if(scenario1 == true) {
throw new MySystemException1("Case 1.");
}
else if(scenario2 == true) {
throw new MySystemException2("Foo not found");
}
}
#Rule solution in JUnit4:
public class ExceptionRule implements MethodRule {
#Override
public Statement apply(final Statement base, final FrameworkMethod method, Object target) {
return new Statement() {
#Override
public void evaluate() throws Throwable {
try {
base.evaluate();
Assert.fail();
} catch (MySystemException e) {
if(scenario1)
assertEquals("Expected error message1", e1.getMessage();
if(scenario2)
assertEquals("Expected error message2",e1.getMessage();
}
}
};
}
}
In your testcase, use the Rule:
#Rule public ExceptionRule rule = new ExceptionRule();
JUnit 4 provides (Expected Exception.class)
#Test(expected= MySystemException.class) public void empty() {
// what ever you want
}
Google: Expected Exceptions JUnit for more info.
BDD Style Solution with Catch Exception
#Test
public void testAMethodScenario1() {
//given scenario 1
when(foo).amethod();
then(caughtException())
.isInstanceOf(MySystemException.class)
.hasMessage("An error occured due to case 1 being incorrect.");
}
#Test
public void testAMethodScenario2() {
//given scenario 2
when(foo).amethod();
then(caughtException())
.isInstanceOf(MySystemException.class)
.hasMessage("An error occured as case 2 could not be found");
}
Source code
https://gist.github.com/mariuszs/7490875
Dependencies
com.googlecode.catch-exception:catch-exception:1.2.0
A better solution with #Rule, you can assert both exception and expection message as well.
#Rule
public ExpectedException expectedException = ExpectedException.none();
#Test
public void aMethod_Scenario1True_ThrowsException() {
expectedException.expect(MySystemException.class);
expectedExcepion.expectMessage("An error occured due to case 1 being incorrect.");
//when().thenReturn();
//handle the repositories, static methods and other sub methods, if needed
amethod();
}
#Rule is the more elegant way to write the exception.