1

I have a wierd problem, I am trying to test a method and expecting it to throw java.util.NoSuchElementException. Here's my test code:

@Test(expected = NoSuchElementException.class) public void testLineWithoutCommas(){ String lineToTest = "[email protected]"; List<String> linesToTest = new ArrayList<String>(); linesToTest.add(lineToTest); applicationChecker = new ApplicationChecker(); applicationChecker.getLinesFromFile(linesToTest); applicationChecker.getDetailsFromLine(applicationChecker.getLines().get(0)); } 

Stack trace looks as expected:

java.util.NoSuchElementException at java.util.StringTokenizer.nextToken(Unknown Source) at java.util.StringTokenizer.nextElement(Unknown Source) at com.scottlogic.cat.util.ApplicationChecker.getDetailsFromLine(ApplicationChecker.java:34) at com.scottlogic.cat.utils.ApplicationCheckerTest.testLineWithoutCommas(ApplicationCheckerTest.java:42) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) at java.lang.reflect.Method.invoke(Unknown Source) and more boring stuff... 

and finally jUnit:

java.lang.AssertionError: Expected exception: java.util.NoSuchElementException at org.junit.internal.runners.statements.ExpectException.evaluate(ExpectException.java:32) at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50) and blah blah blah... 

Any ideas what's wrong? Thanks for any help.

7
  • How are you getting both stack traces? I'd expect to see one or the other, but not both. Commented Jan 13, 2014 at 11:34
  • exactly, right? I have no idea what's wrong with it... Commented Jan 13, 2014 at 11:34
  • Lucas, he means - "how did you get them". Not "isn't that odd". Commented Jan 13, 2014 at 11:37
  • Well this is what I am trying to figure out. I don't know. As far as I know this test should just work and that's it. Commented Jan 13, 2014 at 11:39
  • And to be more specific one of them is console output and the other is jUnit failure trace Commented Jan 13, 2014 at 11:40

2 Answers 2

3

Check you aren't catching and handling the NoSuchElementException somewhere in your code. The first stacktrace could be due to your code catching the exception, then logging it, and throwing it as something else, or just trapping it and not throwing it at all. the 'expected' will only catch exceptions thrown from the test itself, it doesn't deal with exceptions throws and dealt with part way through your code.

Sign up to request clarification or add additional context in comments.

Comments

0

Try it

@Rule public ExpectedException exc = ExpectedException.none(); @Test public void testLineWithoutCommas(){ exc.expect(NoSuchElementException.class); // method have exception 

}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.