[Figure I] is the unit test code?
[Figure II] is the product code?
It is obvious that the unit test code is more than the product code, which is reasonable?
Of course reasonable!
Although the product code is only a few lines; Deal with subscribers ' subscription to the horse racing news?
However, many different user scenarios can be derived; Such as: There is no subscriber subscription, only a single or multiple subscribers, a subscriber duplicate subscription, a Subscriber unsubscribe ...
Unit tests, according to these different user scenarios, have corresponding unit test code (test case)? So, unit test code will naturally be more than the product code?
But is it absolutely worthwhile to pay for it (investment)?
Because, only so the formation of "automated Unit testing" in order to make products in the "shortest possible time feedback", both the structure of the product, the function and quality has been the new code (function) destroyed?
So what we should really focus on is the "validity of Test Cases" unit tests, not the number of lines of the surface unit test code?
PackageTest.Java.com;
Importmain.java.com.Client;
ImportMain.java.com.Message;
ImportMain.java.com.RaceResultsService;
ImportOrg.junit.Before;
Importorg.junit.Test;
Import Staticorg.Mockito.Mockito.Mock;
Import Staticorg.Mockito.Mockito.never;
Import Staticorg.Mockito.Mockito.Verify;
/**
* Created by Scalamind on 2015/3/3.
*/
Public classraceresultsservicetest{
PrivateRaceresultsserviceraceresults;
PrivateMessagemessage;
PrivateClientclienta;
PrivateCLIENTCLIENTB;
@Before
Public voidsetUp() {
Raceresults=NewRaceresultsservice();
message=Mock(Message.class);
Clienta=Mock(Client.class,"Clienta");
CLIENTB=Mock(Client.class,"CLIENTB");
}
//Zero Subscribers
@Test
Public voidNotsubscribeclientshouldnotreceivemessage() {
Raceresults.Send(message);
Verify(Clienta,never()).Receive(message);
Verify(CLIENTB,never()).Receive(message);
}
//One Subscriber
@Test
Public voidSubscribedclientshouldreceivemessage() {
Raceresults.Addsubscriber(Clienta);
Raceresults.Send(message);
Verify(Clienta).Receive(message);
}
Subscribers
@Test
Public voidmessageshouldbesenttoallsubscribedclients() {
RaceresultsserviceRaceresults=NewRaceresultsservice();
Messagemessage= Mock(Message.class);
Raceresults.Addsubscriber(Clienta);
Raceresults.Addsubscriber(CLIENTB);
Raceresults.Send(message);
Verify(Clienta).Receive(message);
Verify(CLIENTB).Receive(message);
}
//Subscribe more than once
@Test
Public voidShouldsendonlyonemessagetomultisubscriber() {
Raceresults.Addsubscriber(Clienta);
Raceresults.Addsubscriber(Clienta);
Raceresults.Send(message);
Verify(Clienta).Receive(message);
}
//Remove a Subscriber
@Test
Public voidunsubscribedclientshouldnotreceivemessages() {
Raceresults.Addsubscriber(Clienta);
Raceresults.Removesubscriber(Clienta);
Raceresults.Send(message);
Verify(Clienta,never()).Receive(message);
}
}
[Figure One: Unit test code]
PackageMain.Java.com;
Importjava.util.Collection;
ImportJava.util.HashSet;
/**
* Created by Scalamind on 2015/3/3.
*/
Public classRaceresultsservice{
PrivateCollection<Client>Clients= NewHashSet<Client> ();
Public voidAddsubscriber(ClientClient) {
Clients.Add(Client);
}
Public voidSend(Messagemessage) {
for(ClientClient: Clients)
Client.Receive(message);
}
Public voidRemovesubscriber(ClientClient) {
Clients.Remove(Client);
}
}
[Figure II: Product Code]
is the unit test code more than the product code?