1

I have class with constructor which gets input like:

public class Project {
    public Project(Parameter1 par1, Parameter2 par2 ...) {
    //here if one incoming parameters equals null - throw exception
    }
}

The question is how can I test that exception is thrown for different parameters within one test? Something like:

@Test
publci void testException() {
    Project project1 = new Project(null, par2 ....);//here it throws  exception and test is finished((((
//I want it to continue testing project2
    Project project2 = new Project(par1, null ...);
}
Thomas Dickey
  • 51,086
  • 7
  • 70
  • 105
ovod
  • 1,118
  • 4
  • 18
  • 33
  • That looks right to me. What problem is it causing for you? – Sam Estep Jun 27 '15 at 22:57
  • My class has 5 parameters input contractor. I want to test them all in one test. project2, project3 and so on. Not stop at first but verify that in all cases Project throws exception. This is the problem. – ovod Jun 27 '15 at 23:01
  • I'm not sure that's possible. Often, you'll find that you need multiple tests to test a single function completely. – Sam Estep Jun 27 '15 at 23:03
  • 2
    While you can wrap each invocation inside a `try-catch` block, you really shouldn't test more than one constructor in a method. Why not make multiple methods, each named accordingly like `constructWithNullParam2ShouldthrowNPE()` etc – dkatzel Jun 28 '15 at 00:30
  • I dont think that is a good idea. If my constructor has 100 parameters but only one checking *If* structure? It is wider to test *if* in one test not make 100 tests to test the same thing. No? – ovod Jun 28 '15 at 12:58
  • A well designed class does not have such much constructor parameters. – Stefan Birkner Jul 03 '15 at 21:16
  • possible duplicate of [Testing for multiple exceptions with JUnit 4 annotations](http://stackoverflow.com/questions/1410172/testing-for-multiple-exceptions-with-junit-4-annotations) – Joe Aug 09 '15 at 09:02

4 Answers4

3
@Test
public void testException() {
    boolean exception1Thrown = false;
    try {
        Project project1 = new Project(null, par2 ....);
    }catch(Exception e){
        exception1Thrown = true;
    }
    assertTrue(exception1Thrown);

    boolean exception2Thrown = false;
    try {
        Project project2 = new Project(par1, null ...);
    }catch(Exception e){
        exception2Thrown = true;
    }
    assertTrue(exception2Thrown);

}

Thats just one of several ways to do it. See this question for more

Community
  • 1
  • 1
Tom Hanley
  • 1,252
  • 11
  • 21
1

Keep Project1 = new Project(.... and Project2 = new Project(..... in their individual try catch blocks. Exception thrown through first block will not stop later section of code to run.

Raman Shrivastava
  • 2,923
  • 15
  • 26
0

you could do it passing flag (shouldThrowException) as one of the test parameters. but much cleaner way would be to make two tests. one for correct parameters and one for bad parameters. i would do this:

import static org.assertj.core.api.Assertions.assertThatThrownBy;
import org.junit.runner.RunWith;
import com.googlecode.zohhak.api.Coercion;
import com.googlecode.zohhak.api.TestWith;
import com.googlecode.zohhak.api.runners.ZohhakRunner;

@RunWith(ZohhakRunner.class)
public class MyTest {

  @TestWith({
        "parameter1,      parameter2",
        "otherParameter1, otherParameter2" 
  })
  public void should_construct_project(Parameter parameter1, Parameter parameter2) {
    new Project(parameter1, parameter2);
  }

  @TestWith({
        "null,            parameter2",
        "otherParameter1, null",
        "badParameter1,   goodParameter2"
  })
  public void should_fail_constructing_project(Parameter parameter1, Parameter parameter2) {

    assertThatThrownBy(() -> new Project(parameter1, parameter2))
                                    .isInstanceOf(NullPointerException.class);          
  }

  @Coercion
  public Parameter toParameter(String input) {
    return new Parameter(...);
  }
}

in case you want to test all possible parameters combinations then data-providers or theories could be useful.

piotrek
  • 13,982
  • 13
  • 79
  • 165
0

You can use https://github.com/Pragmatists/JUnitParams to do this:

Say you had a Person object were all parameters must be specified then you can test in this way with JUnitParams:

@Test(expected = IllegalArgumentException.class)
    @Parameters(
    {", bloggs, joe.bloggs@ig.com",
    "joe, , joe.bloggs@ig.com,",
    "joe, bloggs, ,",
)
public void allParametersAreMandatory(String firstName, String lastName, String emailAddress)
{
   new Person(firstName, lastName, emailAddress);
}
Big Kahuna
  • 545
  • 4
  • 12