Junit Test Methods

What are the most commonly used testing methods that I should start with in order to become familiar with unit testing? There are a lot of them, but I think that there is something like ordinary or something like that.

I meant Junit methods like AssertTrue (), etc.

+4
source share
5 answers

There are just a few templates to learn, with several implementation methods for different types and an optional argument to the initial message.

  • assertEquals ()
  • assertTrue () and assertFalse ()
  • assertNull () and assertNotNull ()
  • assertSame () and assertNotSame ()
  • does not work()
  • assertArrayEquals ()
  • assertThat ()

At a minimum, you need to learn all the templates, but the last - all this is necessary for different situations.

+5
source

assertEquals is the most commonly used testing method.

 assertEquals( "string1", "string1" ); //would fail assertEquals( expectedValue, actualValue ); //would pass if expectedValue.equals( actualValue ) 

You can also add a comment that prints if the statement fails:

 assertEquals( "method result should be 7", 7, thing.methodThatShouldReturn7() ); //would pass if 7 == thing.methodThatShouldReturn7() 

See Assert class javadoc for more details, and as soon as you feel comfortable with assertEquals, you can see the other options available to you.

+4
source

setUp () and tearDown (), they are called before and after each case.

+1
source

I would also recommend knowing about fail () and exception handling in JUnit. A brief suggestion is to always exclude exceptions from the testing method, if not to test this particular exception. Capturing exceptions and crashes, but you are losing quite a bit of reporting information. A good article on this is here: http://www.exubero.com/junit/antipatterns.html .

+1
source

@Before and @ After annotations (equivalent to those for setUp () and tearDown ()).

0
source

All Articles