开发者

Junit and messageboxes

开发者 https://www.devze.com 2023-03-18 15:01 出处:网络
I am new to JUnit and I got a sample java project in which I need to write unit tests for all the methods.

I am new to JUnit and I got a sample java project in which I need to write unit tests for all the methods. Unfortunately the code is poorly designed and some of the methods are done from the UI. Furthermore, some of the methods pop up a messagebox and do not return a return value.

I have two questions: First, without modifying the existing code, is there a way I can suppress the message boxes and not press enter every time I run the unit tests?

Second question: can a test function expect a message box and assert failure\success upon it's string content?

I appreciate any help, I know the best solution is to fix the code itself - separate the BusinessLogic completely from the UI and to test expected result, or even if message boxes are somehow mandatory use modal message boxes (like humble dialog boxes) but unfortunately I am not allo开发者_运维技巧wed to change anything in the code.

Thanks :)

Nili


There are all sorts of ways you could get started if only you were allowed to edit the code, so my first approach would be to see if you can get this restriction relaxed, and to read Working Effectively With Legacy Code.

Failing that you could try using a GUI testing framework like FEST-Swing to check the contents of the message boxes are as expected.


Not allowed to change the code, you say? First thought it to have a look at JMockit which really opens up a lot of possibilities when you are severely constrained by code that was not written with much concern about how it should be tested. It should enable you to, without modifying any code, substitute your preferred implementation of bothersome parts while your test is running--so only in the context of testing would you have altered the test subject (be careful to write a meaningful test!) or its dependencies. Other mock object frameworks can be useful, too, but the investment to learn JMockit is really time well-spent.


unfortunately I am not allowed to change anything in the code.

There's all sorts of stuff on Google about how to automate Swing testing with JUnit. Unfortunately, there's no way to get around this problem when testing.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号