code_junkie

Proper unit testing: testing a method with no [accessible] state or return value

Nov 13th, 2011
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.38 KB | None | 0 0
  1. public static void main(String[] args)
  2. {
  3. Server myServer = new Server()
  4. if(myServer.start())
  5. System.out.println("started");
  6. else
  7. System.out.println("failed");
  8. }
  9.  
  10. static void Main()
  11. {
  12. Application.EnableVisualStyles();
  13. Application.SetCompatibleTextRenderingDefault(false);
  14. Application.Run(new MainWindow());
  15. }
  16.  
  17. static void Main()
  18. {
  19. new MainApp().Run();
  20. }
  21.  
  22. public class MainApp
  23. {
  24. private Server m_Server;
  25. public MainApp():this(new Server())
  26. {}
  27. public MainApp(Server s)
  28. { m_Server = s; }
  29.  
  30. public void Run()
  31. { Log(LaunchServer()); }
  32.  
  33. private string LaunchServer()
  34. { return (m_Server.start() ? "started" : "failed"); }
  35.  
  36. protected virtual void Log(string sMessage)
  37. { System.Console.WriteLine(sMessage); }
  38. }
  39.  
  40. public class FakeApp : MainApp
  41. {
  42. private string sLastLoggedMessage;
  43.  
  44. public FakeApp(Server s) : base(s) { }
  45.  
  46. public string getLastLoggedMessage()
  47. { return sLastLoggedMessage; }
  48.  
  49. protected override void Log(string sMessage)
  50. { sLastLoggedMessage = sMessage; }
  51. }
  52.  
  53. [TestFixture]
  54. public class TestMainApp
  55. {
  56. [Test]
  57. public void TestRun()
  58. {
  59. Server s = new Server();
  60. FakeApp f = new FakeApp(s);
  61.  
  62. f.Run();
  63.  
  64. Assert.AreEqual("started", f.getLastLoggedMessage());
  65. }
  66. }
  67.  
  68. Server myServer = new Server();
  69.  
  70. Server myServer = applicationContext.getBean("server");
Add Comment
Please, Sign In to add comment