-8

I need a test case for the exception part:

try { 
  return String.valueOf(value);
} catch (Exception e) {
  return null;
}
Draken
  • 3,134
  • 13
  • 34
  • 54
  • 2
    so write one. What are you waiting for? – Scary Wombat Sep 14 '16 at 06:31
  • Possible duplicate of [How do you assert that a certain exception is thrown in JUnit 4 tests?](http://stackoverflow.com/questions/156503/how-do-you-assert-that-a-certain-exception-is-thrown-in-junit-4-tests) – default locale Sep 14 '16 at 06:31
  • Pass in a mocked `value` which throws an exception when you call its `toString()` method. – Andy Turner Sep 14 '16 at 06:50
  • @AndyTurner Why did you delete your answer? doesnt look too bad to me? Although, sometimes I too think that answering this low-quality questions isnt really what the community expects what should happen. – GhostCat Sep 14 '16 at 06:54
  • 1
    @GhostCat that was the point. What the hey, have the answer back. I feel there is sufficient "don't do that" value (see below the `---`). – Andy Turner Sep 14 '16 at 07:09

1 Answers1

2

You can create a class whose toString() method throws an exception:

class Throwing {
  @Override public String toString() {
    throw new UnsupportedOperationException();
  }
}

and then pass an instance of this to your method in the unit test:

assertNull(yourMethod(new Throwing()));

Of course, you can use your favorite mocking framework rather than creating an explicit class like this.


But think very carefully about whether this method is worthwhile. It is a pretty big source of problems if you have classes whose toString() methods throw exceptions, and that's the only case that your method handles over and above String.valueOf.

In any other place in the code where you happen to perform a string conversion (e.g. "" + instance), instances of that class will throw an exception, and you won't be catching them there.

Consider this insidious case:

try {
  doSomething(instance);
} catch (Exception e) {
  throw new RuntimeException("Exception occurred: " + instance, e);
}

In this case, the exception from instance.toString() would only be thrown if an exception e is caught, but it's the exception from toString() which would be thrown - you'd not actually see e at all. Thus, it would make it much harder to see the real problem.

I would recommend not having this method at all, so not catching the exception, and using String.valueOf(Object) directly (or implicitly), so that you actually know when a problem occurs.

Unchecked exceptions - the only type which can be thrown by toString() - indicate programming errors. Don't swallow those errors, then you'll know to fix them.

Andy Turner
  • 137,514
  • 11
  • 162
  • 243
  • hi all i got my answer by try for a long time, below is test case which is going to test string for null: public void toStringForNULLTest() throws Exception { new NonStrictExpectations() { { new MockUp() { @Mock public String valueOf(Object obj) throws Exception { throw new Exception(); } }; } }; Integer a= new Integer(123); utilities.toString(a); } – ShaRmaVikRam Oct 07 '16 at 03:42
  • 1
    You should really update your question and not put that in a comment. – Steve May 25 '17 at 05:57