is the unit test code more than the product code?

Source: Internet
Author: User

[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?

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.