JUnit validation with wait expectations (multiple statements)

I am testing WeekConverter to use Xalan and wondering what exactly my test is doing .: D

Having the following testing method:

@Test(expected = IllegalArgumentException.class) public void testConvertTwoDigitYearWithWrongInput() { WeekConverter weekConverter = new WeekConverter(WeekConverter.Strategy.TWO_DIGIT_YEAR); //wrong or empty inputs assertEquals("0", weekConverter.convert("")); assertEquals("0", weekConverter.convert("abcdefgh")); } 

Will this test expect an exception for all statements, or only for the first statement? If only the first, which would mean that I have to create a test method for each statement, although I expect the same exception in both cases. Can someone confirm my example here, please?

I also have a test for null that throws a NullPointerException. A soft check is as follows:

 if (inputDate == null) { do something and throw NullPointerexception } else if (inputDate.isEmpty()) { do something and throw IllegalArgumentException, since inputDate is not really null } else if (inputDate.matches(regex)) { go futher and convert } else { do something and throw IllegalArgumentException, since inputDate does not match regex } 

Therefore, one test method expects an IllegalArgumentException with two statements. But obviously, I need two different testing methods, not just to respect the functionality of JUnit, but also that I expect a throw from two different states.

+4
source share
5 answers

You can split your method into several methods, but if you have many input samples, this will be inconvenient.

Instead, you can use the following approach:

 @Test public void testConvertTwoDigitYearWithWrongInput() { WeekConverter weekConverter = new WeekConverter(WeekConverter.Strategy.TWO_DIGIT_YEAR); assertFailsToConvert(weekConverter, ""); assertFailsToConvert(weekConverter, "abcdefgh"); } private void assertFailsToConvert(WeekConverter weekConverter, String input) { try { weekConverter.convert(input); fail("Should not convert [" + input + "]"); } catch (IllegalArgumentException ex) {} } 
+3
source

You must provide several testing methods as they test different things.

An exception will be thrown the first time the converter receives an illegal argument.

You should also check for null input, just to document the behavior.

+1
source

The test simply expects an IllegalArgumentException to be thrown no matter where and why it is thrown.

I recommend you break it into two tests.

+1
source

You can put the creation of the conversion device in a separate @Before setup method, and then you can have (three) separate test cases for working with null, "and" abcdef ".

If there are more cases to check, the neat way in JUnit is to use the @Parameters annotation and the corresponding runner.

Your test class will deal with incorrect two-digit years. Its constructor will be parameterized using inputDate type String.

The static method giving @Parameters returns a collection containing "" and abcdefg (and other funny cases).

In one test case, an IllegalArgumentException is expected.

 @RunWith(Parameterized.class) public class IncorrectTwoDigitYears { String inputDate; public IncorrectTwoDigitYears(String inputDate) { this.inputDate = inputDate; } @Test(expected = IllegalArgumentException.class) public void testFormat() { (new WeekConverter(WeekConverter.Strategy.TWO_DIGIT_YEAR)) .convert(inputDate); } @Parameters public static Collection<Object[]> data() { Object[][] data = new Object[][] { { "" }, { "abcdef" }, { "0" }, { "000" }, { "##" } }; return Arrays.asList(data); } } 

The payout will be higher if you have more than two cases for testing.

+1
source

Try catch-exception :

 @Test public void testConvertTwoDigitYearWithWrongInput() { WeekConverter weekConverter = ... // wrong or empty inputs verifyException(weekConverter, IllegalArgumentException.class) .convert(""); verifyException(weekConverter, IllegalArgumentException.class) .convert("abcdefgh"); } 
+1
source

Source: https://habr.com/ru/post/1387225/


All Articles