java - Should I write tests for class A if it's covered from class B -


i'd opinion testing methodology.

lets assume have class , b. class b uses functionality of class a. b class tested , test coverage applied indirectly class a.

should write full tests directly class a? or should test not tested class functionality?

i asking because maybe in future there possibility b class removed or modified in way might not use same functionality class might leave methods untested. do?

classes != units

if practice tdd, understand behind.


imo, should test b's behavior without based on fact a tested.

actually, there 3 cases:

a , b belonging same layer:

  • if a created through refactor cycle (extract class) of b (happens while practicing tdd), a should totally left untested! no need test @ all!
    indeed, structure of code (in case, separation of classes/srp) should independent of unit concept; b , a in case belonging same unit.

  • if a existed before b, b should not based on fact, , b's whole behavior should tested.

a , b not belonging same layer (distinct boundaries instance):

  • if b gui class, , a business class, a should doubled/mocked when testing b, , a should have full test dedicated it.
    indeed, domain architecture should not mingled behavior/feature notion.

to understand why, read recent article of uncle bob dealing concept:

http://blog.8thlight.com/uncle-bob/2014/01/27/thechickenortheroad.html?utm_source=hootsuite&utm_campaign=hootsuite

excerpt of it:

it common misconception design of tests must mirror design of production code. tdd not require, author suggests, "that every unit in system paired well-designed [...] unit test." indeed, that's 1 of reasons many of have stopped calling them "unit" tests.

note: tdd doesn't care "future", in contrary, helps write code need, no more. therefore should not worry this:

in future there possibility b class removed or modified

if wrote tests (i prefer word "specs"), such removal detected immediately.


Comments

Popular posts from this blog

html - Sizing a high-res image (~8MB) to display entirely in a small div (circular, diameter 100px) -

java - IntelliJ - No such instance method -

identifier - Is it possible for an html5 document to have two ids? -