How do I generate a stream from a string?

public static Stream GenerateStreamFromString(string s) { var stream = new MemoryStream(); var writer = new StreamWriter(stream); writer.Write(s); writer.Flush(); stream.Position = 0; return stream; } Don’t forget to use Using: using (var stream = GenerateStreamFromString(“a,b \n c,d”)) { // … Do stuff to stream } About the StreamWriter not being disposed. StreamWriter is just a wrapper … Read more

How to mock void methods with Mockito

Take a look at the Mockito API docs. As the linked document mentions (Point # 12) you can use any of the doThrow(),doAnswer(),doNothing(),doReturn() family of methods from Mockito framework to mock void methods. For example, Mockito.doThrow(new Exception()).when(instance).methodName(); or if you want to combine it with follow-up behavior, Mockito.doThrow(new Exception()).doNothing().when(instance).methodName(); Presuming that you are looking at … Read more

How do I test a class that has private methods, fields or inner classes?

If you have somewhat of a legacy Java application, and you’re not allowed to change the visibility of your methods, the best way to test private methods is to use reflection. Internally we’re using helpers to get/set private and private static variables as well as invoke private and private static methods. The following patterns will … Read more