unit test candidate solutions
TRANSCRIPT
![Page 1: Unit test candidate solutions](https://reader036.vdocuments.net/reader036/viewer/2022062300/554f4b0eb4c905524c8b4962/html5/thumbnails/1.jpg)
Unit Test Candidate Solutions
Xuefeng.Wu 2012.05
![Page 2: Unit test candidate solutions](https://reader036.vdocuments.net/reader036/viewer/2022062300/554f4b0eb4c905524c8b4962/html5/thumbnails/2.jpg)
Types of Tests
![Page 3: Unit test candidate solutions](https://reader036.vdocuments.net/reader036/viewer/2022062300/554f4b0eb4c905524c8b4962/html5/thumbnails/3.jpg)
Types of Tests
Unit test use mock environment : isolationsingle responsibility
Integration test use simulation 3rd applications, system ,drivers
Functional test use real 3rd applications, system , drivers
![Page 4: Unit test candidate solutions](https://reader036.vdocuments.net/reader036/viewer/2022062300/554f4b0eb4c905524c8b4962/html5/thumbnails/4.jpg)
Test context
CSDM
CSDM Demo, CSDM
Controller
CSDM Service
Soap services
Domain Manager
Business logic managers
![Page 5: Unit test candidate solutions](https://reader036.vdocuments.net/reader036/viewer/2022062300/554f4b0eb4c905524c8b4962/html5/thumbnails/5.jpg)
Test Targets
![Page 6: Unit test candidate solutions](https://reader036.vdocuments.net/reader036/viewer/2022062300/554f4b0eb4c905524c8b4962/html5/thumbnails/6.jpg)
Technology Problems
• Environment and Context for developer • Test database prepare for developer • Test sample files prepare for developer• XML String parameter in service for soap services test• XML verify for soap services test• Mock Object for domain logic test• Model Date verify for domain logic test• Files verify for domain logic test• Notification verify for domain logic test• Unit testing styles for better test code
![Page 7: Unit test candidate solutions](https://reader036.vdocuments.net/reader036/viewer/2022062300/554f4b0eb4c905524c8b4962/html5/thumbnails/7.jpg)
Test database prepare
• DB Unit/ unitils• Derby backup, restore on line• Derby date file recovery with RAM Disk
![Page 8: Unit test candidate solutions](https://reader036.vdocuments.net/reader036/viewer/2022062300/554f4b0eb4c905524c8b4962/html5/thumbnails/8.jpg)
Test database prepare
• Baseline data• Each Test data is individual and isolation
![Page 9: Unit test candidate solutions](https://reader036.vdocuments.net/reader036/viewer/2022062300/554f4b0eb4c905524c8b4962/html5/thumbnails/9.jpg)
DB Unit
• DbUnit has the ability to export and import your database data to and from XML datasets.
public DBUnitSampleTest(String name) { super( name ); System.setProperty( PropertiesBasedJdbcDatabaseTester.DBUNIT_DRIVER_CLASS, "org.apache.derby.jdbc.ClientDriver" ); System.setProperty( PropertiesBasedJdbcDatabaseTester.DBUNIT_CONNECTION_URL, "jdbc:derby://localhost:1527/pas;create=true" );}
protected IDataSet getDataSet() throws Exception {
return new FlatXmlDataSetBuilder().build(new FileInputStream("D:/Test/pas.xml"));
}
protected DatabaseOperation getSetUpOperation() throws Exception { return DatabaseOperation.REFRESH; }
protected DatabaseOperation getTearDownOperation() throws Exception { return DatabaseOperation.NONE; }
Perform setup of stale data once for entire test class or test suite
![Page 10: Unit test candidate solutions](https://reader036.vdocuments.net/reader036/viewer/2022062300/554f4b0eb4c905524c8b4962/html5/thumbnails/10.jpg)
Derby backup, restore on line
String dbURL = "jdbc:derby:salesdb;restoreFrom=D:/dbbackups/salesdb"; Connection conn = DriverManager.getConnection(dbURL);
![Page 11: Unit test candidate solutions](https://reader036.vdocuments.net/reader036/viewer/2022062300/554f4b0eb4c905524c8b4962/html5/thumbnails/11.jpg)
Derby date file recovery
• Hey! We use derby!• Use Embedded Derbey• Restore data files which in RAM Disk
<property name="hibernate.connection.url">jdbc:derby:database/pas;create=true</property>
<property name="hibernate.connection.driver_class">org.apache.derby.jdbc.EmbeddedDriver</property>
![Page 12: Unit test candidate solutions](https://reader036.vdocuments.net/reader036/viewer/2022062300/554f4b0eb4c905524c8b4962/html5/thumbnails/12.jpg)
Test sample files prepare
• Dicom files,images,3D files• Custom file providers
![Page 13: Unit test candidate solutions](https://reader036.vdocuments.net/reader036/viewer/2022062300/554f4b0eb4c905524c8b4962/html5/thumbnails/13.jpg)
XML String parameter in service
• Soap service is all about xml string in and out• But it's not easy to provide XML format
string in Java.
![Page 14: Unit test candidate solutions](https://reader036.vdocuments.net/reader036/viewer/2022062300/554f4b0eb4c905524c8b4962/html5/thumbnails/14.jpg)
Java StringBuffer
String buildXML() { StringBuffer sb = new StringBuffer(); sb.append("<EXPRESSION>"); sb.append(" <EQUAL>"); sb.append(" <IDENT>x</IDENT>"); sb.append(" <PLUS>"); sb.append(" <INT>3</INT>"); sb.append(" <TIMES>"); sb.append(" <INT>4</INT>"); sb.append(" <INT>5</INT>"); sb.append(" </TIMES>"); sb.append(" </PLUS>"); sb.append(" </EQUAL>"); sb.append("</EXPRESSION>"); return sb.toString(); } // buildXML
![Page 15: Unit test candidate solutions](https://reader036.vdocuments.net/reader036/viewer/2022062300/554f4b0eb4c905524c8b4962/html5/thumbnails/15.jpg)
Read xml file as input
readFileToString.java
![Page 16: Unit test candidate solutions](https://reader036.vdocuments.net/reader036/viewer/2022062300/554f4b0eb4c905524c8b4962/html5/thumbnails/16.jpg)
Use Other Language raw string
val input = """ <trophy type="request" version="1.0"> <patient> <parameter key="patient_id" value="" /> C (If patient_id and DPMS_id are both null, PAS shall set Patient=patient_internal_id and DPMS_id=0.) <parameter key="dpms_id" value=""/> C (If both parameter are not null, use input value. Only above two conditions, others will be take as exception.) <parameter key="first_name" value=""/> O <parameter key="last_name" value=""/> O <parameter key="middle_name" value=""/> O <parameter key="prefix" value=""/> O <parameter key="suffix" value=""/> O <parameter key="birth_date" value=""/> O ("yyyy-MM-dd") <parameter key="sex" value=""/> O (male,female,other) <parameter key="pregnancy" value=""/> O (not pregnant,possibly pregnant,definitively pregnant,unknown) empty string means not application. <parameter key="insurance_number" value=""/> O <parameter key="address" value=""/> O <parameter key="town" value=""/> O <parameter key="postal_code" value=""/> O <parameter key="cellular_phone" value=""/> O <parameter key="home_phone" value=""/> O <parameter key="work_phone" value=""/> O <parameter key="comments" value=""/> O <parameter key="email" value=""/> O <parameter key="photo" value=""/> O <parameter key="kdis6_patient_directory" value=""/> O (used for import kdis6 images) <parameter key="images_imported" value=""/> C ("true/false", must be pair with kdis6_patient_directory) </patient> </trophy> """
![Page 17: Unit test candidate solutions](https://reader036.vdocuments.net/reader036/viewer/2022062300/554f4b0eb4c905524c8b4962/html5/thumbnails/17.jpg)
XML verify
• The soap service output is xml string• You can use contains to verify simple value• But not easy to verify specific value
![Page 18: Unit test candidate solutions](https://reader036.vdocuments.net/reader036/viewer/2022062300/554f4b0eb4c905524c8b4962/html5/thumbnails/18.jpg)
XmlUnit
• XML can be used for just about anything so deciding if two documents are equal to each other isn't as easy as a character for character match
public void testXPathValues() throws Exception { String myJavaFlavours = "<java-flavours><jvm current='some platforms'>1.1.x</jvm>" + "<jvm current='no'>1.2.x</jvm><jvm current='yes'>1.3.x</jvm>" + "<jvm current='yes' latest='yes'>1.4.x</jvm></java-flavours>";
assertXpathEvaluatesTo("1.4.x", "//jvm[@latest='yes']", myJavaFlavours); assertXpathEvaluatesTo("2", "count(//jvm[@current='yes'])", myJavaFlavours); assertXpathValuesEqual("//jvm[4]/@latest", "//jvm[4]/@current", myJavaFlavours); assertXpathValuesNotEqual("//jvm[2]/@current", "//jvm[3]/@current", myJavaFlavours); }
![Page 19: Unit test candidate solutions](https://reader036.vdocuments.net/reader036/viewer/2022062300/554f4b0eb4c905524c8b4962/html5/thumbnails/19.jpg)
Scala XML
val ns = <foo><bar><baz/>Text</bar><bin/></foo>
ns \ "foo" // => <foo><bar>...</bar><bin/></foo>
ns \ "foo" \ "bar" // => <bar><baz/>Text</bar>
val ns = <foo id="bar"/> ns \ "@id" // => Text(bar)
![Page 20: Unit test candidate solutions](https://reader036.vdocuments.net/reader036/viewer/2022062300/554f4b0eb4c905524c8b4962/html5/thumbnails/20.jpg)
JRuby REXML
xml = File.read('posts.xml') puts Benchmark.measure {
doc, posts = REXML::Document.new(xml), []
doc.elements.each('posts/post') do |p| posts << p.attributes
End}
![Page 21: Unit test candidate solutions](https://reader036.vdocuments.net/reader036/viewer/2022062300/554f4b0eb4c905524c8b4962/html5/thumbnails/21.jpg)
Model Date verify
• After execute the method you want to know every thing is correct.
![Page 22: Unit test candidate solutions](https://reader036.vdocuments.net/reader036/viewer/2022062300/554f4b0eb4c905524c8b4962/html5/thumbnails/22.jpg)
Solutions
• DbUnit can also help you to verify that your database data match an expected set of values.
• Call hibernate Dao Directly• Verify by other business method
![Page 23: Unit test candidate solutions](https://reader036.vdocuments.net/reader036/viewer/2022062300/554f4b0eb4c905524c8b4962/html5/thumbnails/23.jpg)
Files verify
• Custom utile
• public static void checkDuplicateFiles(File dir)
![Page 24: Unit test candidate solutions](https://reader036.vdocuments.net/reader036/viewer/2022062300/554f4b0eb4c905524c8b4962/html5/thumbnails/24.jpg)
Notification verify
• An ActiveMQ client utile• It should run in other thread and subscript
before call method• It should time out if no message received after
long time
![Page 25: Unit test candidate solutions](https://reader036.vdocuments.net/reader036/viewer/2022062300/554f4b0eb4c905524c8b4962/html5/thumbnails/25.jpg)
Wait future notification
"There should have imageCreated notification after image is created " should {
" ok " in {
val srcPid = DataPrepare.createPatient val msg = future(ActiveMQ.receive("topic.imageCreated")) val imgId = DataPrepare.prepareImage(srcPid)._1 //wait a 3 second awaitAll(3000,msg) foreach{ v => v match { case Some(m) => { m.toString must contain (srcPid)
m.toString must contain (imgId) } case None => 1 must_== 2 } }}
}
![Page 27: Unit test candidate solutions](https://reader036.vdocuments.net/reader036/viewer/2022062300/554f4b0eb4c905524c8b4962/html5/thumbnails/27.jpg)
EasyMock
public void testTransferShouldDebitSourceAccount(){
AccountController controller = new AccountController();
Account from = createMock(Account.class)
Account to = createMock(Account.class)
from.debit(42);
replay(from);
replay(to);
controller.transfer(from,to,42);
verify(from);
}
![Page 28: Unit test candidate solutions](https://reader036.vdocuments.net/reader036/viewer/2022062300/554f4b0eb4c905524c8b4962/html5/thumbnails/28.jpg)
jMock
Mockery context = new Mockery(); public void testTransferShouldDebitSourceAccount(){
AccountController controller = new AccountController();
Account from = context.mock(Account.class)
Account to = context.mock(Account.class)
context.checking(new Expectations() {{
oneOf(from).debit(42);
}}
controller.transfer(from,to,42);
context.assertIsSatisfied();
}
![Page 29: Unit test candidate solutions](https://reader036.vdocuments.net/reader036/viewer/2022062300/554f4b0eb4c905524c8b4962/html5/thumbnails/29.jpg)
mockito
public void testTransferShouldDebitSourceAccount(){
AccountController controller = new AccountController();
Account from = mock(Account.class)
Account to = mock(Account.class)
controller.transfer(from,to,42);
verify(from).debit(42);
}
![Page 30: Unit test candidate solutions](https://reader036.vdocuments.net/reader036/viewer/2022062300/554f4b0eb4c905524c8b4962/html5/thumbnails/30.jpg)
public void testTransferShouldDebitSourceAccount(){
AccountController controller = new AccountController();
Account from = createMock(Account.class)
Account to = createMock(Account.class)
from.debit(42);
replay(from);
replay(to);
controller.transfer(from,to,42);
verify(from); }
Mockery context = new Mockery(); public void testTransferShouldDebitSourceAccount(){
AccountController controller = new AccountController();
Account from = context.mock(Account.class)
Account to = context.mock(Account.class)
context.checking(new Expectations() {{ oneOf(from).debit(42);
}}
controller.transfer(from,to,42);
context.assertIsSatisfied(); }
public void testTransferShouldDebitSourceAccount(){
AccountController controller = new AccountController();
Account from = mock(Account.class)
Account to = mock(Account.class)
controller.transfer(from,to,42);
verify(from).debit(42);
}
![Page 31: Unit test candidate solutions](https://reader036.vdocuments.net/reader036/viewer/2022062300/554f4b0eb4c905524c8b4962/html5/thumbnails/31.jpg)
Mock what?
![Page 32: Unit test candidate solutions](https://reader036.vdocuments.net/reader036/viewer/2022062300/554f4b0eb4c905524c8b4962/html5/thumbnails/32.jpg)
Environment and Context
Name Location Interaction
DB Internal JDBC connect with hibernate
ActiveMQ Internal socket,ActiveMQ library
Demo External cmd,socket
Config file External read/write file
Registration table External system tool,reg
Schedule External trigger
2D External cmd,socket
Object Applications External cmd
![Page 33: Unit test candidate solutions](https://reader036.vdocuments.net/reader036/viewer/2022062300/554f4b0eb4c905524c8b4962/html5/thumbnails/33.jpg)
Unit testing styles
• BDD• Better test code• It’s better not only for developer. If P.O. can
read our test code is better • The test code should like specifics
![Page 34: Unit test candidate solutions](https://reader036.vdocuments.net/reader036/viewer/2022062300/554f4b0eb4c905524c8b4962/html5/thumbnails/34.jpg)
Traditional Status assert test
public void testAdd() {
int num1 = 3;
int num2 = 2;
int total = 5;
int sum = 0;
sum = Math.add(num1, num2);
assertEquals(sum, total);
}
![Page 35: Unit test candidate solutions](https://reader036.vdocuments.net/reader036/viewer/2022062300/554f4b0eb4c905524c8b4962/html5/thumbnails/35.jpg)
BDD specifications style class HelloWorldSpec extends Specification {
"The 'Hello world' string" should {
"contain 11 characters" in {
"Hello world" must have size(11) }
"start with 'Hello'" in {
"Hello world" must startWith("Hello") }
"end with 'world'" in {
"Hello world" must endWith("world") }
}
"The sum" should {“ equals total " in { val num1 = 2,num2 = 3, total = 5
Math.add(num1, num2) must equalsTo(total) }
}
}
![Page 36: Unit test candidate solutions](https://reader036.vdocuments.net/reader036/viewer/2022062300/554f4b0eb4c905524c8b4962/html5/thumbnails/36.jpg)
![Page 37: Unit test candidate solutions](https://reader036.vdocuments.net/reader036/viewer/2022062300/554f4b0eb4c905524c8b4962/html5/thumbnails/37.jpg)