java - jUnit Usage of setUp and tearDown -


this question has answer here:

being new junit have done far setting dependencies (i.e. creating objects) within test methods itself.

questions:

  1. eclipse mocks unused variables, though. setup , teardown for?

  2. is practice create objects within setup , null them out via teardown?

  3. what other use cases methods above?

  4. what purpose of working pre-suite setup , teardown. can give example when comes in handy?

cheers, andrew

being new junit have done far setting dependencies (i.e. creating objects) within test methods itself.

if initialized , cleaned correctly there nothing wrong approach. have way, if different tests need different dependencies.

eclipse mocks unused variables, though. setup , teardown for?

unused variables have nothing setup , teardown methods. should either use them or remove them.

is practice create objects within setup

in cases setup method (or nowadays @before annotation) necessary. constructor , inline initializes work well. @before annotation useful, if have inheritance in tests or want take advantage of @rules during initialization.

and null them out via teardown?

this bad idea. teardown (or nowadays @after annotation) should used clean external resources connections , files or revert changes made static state of application. there no need null fields garbage collector reclaim them anyway.

what purpose of working pre-suite setup , teardown. can give example when comes in handy?

sometimes want share resources between tests. example slow create database connections. suite methods let create them once per suite instead of once per test or per test class.


Comments

Popular posts from this blog

powershell Start-Process exit code -1073741502 when used with Credential from a windows service environment -

twig - Using Twigbridge in a Laravel 5.1 Package -

c# - LINQ join Entities from HashSet's, Join vs Dictionary vs HashSet performance -