0

I am quite new to Junit, I am currently trying to test out some APIs corresponding to a simple UI with many checkboxes and a couple textfields.

what I have in mind now is to write something with the following structure:

testButtonOne()

testButtonTwo()

. . .

testButtonTen()

testCheckboxOne() with many inputs such as space, invalid char, very long string etc

testCheckboxTwo() with many inputs such as space, invalid char, very long string etc

I have tried to write the test in two files, one being single runs and one being parameterized runs, but the problem is that I have to write @BeforeClass @Before @After twice, so I am wondering what would be a better way to write test like this.

guberland
  • 55
  • 6

2 Answers2

1

You should use either https://github.com/junit-team/junit4/wiki/parameterized-tests

Or better, I think you should look at https://github.com/pholser/junit-quickcheck

Sample project using junit-quickcheck see this example github project https://github.com/bfayette/lottery/blob/master/src/test/java/com/silanis/lottery/PrizeCalculatorTest.java

Breton F.
  • 177
  • 1
  • 6
0

You can look here: Testing GUI with JUnit

The main idea is that it is preferred to decouple GUI from business logic and other layers. In this way GUI layer will contain only GUI specific code, so testing will become easier and it's value will decrease. That way you can mock your services in GUI class and just verify, for example, that if you enter some text in EditBox than it's value will go as a parameter to specific service call (in this case I don't think you'll need to write parametrized unit tests), and then you can test service layer separately. You can have your validation service, and test it separately as well. Using such approach tests usually become cleaner and more specific, not so broad, which is the idea laying behind unit tests.

If you still need parametrized unit tests for testing GUI, you can have both parametrized and non-parametrized tests, grouped in nested classes of one test class as described here: https://stackoverflow.com/a/28203229/9329760

gmpf
  • 236
  • 2
  • 11