What's the difference between DOM and SAX? DOM creates tree-like representation of the XML document in memory, SAX is event-based. 
What's the difference between XSD and DTD? XSD is in XML, DTD is not. XSD is much more comprehensive than DTD
You're given an XML file, and you're supposed to retrieve the value of a specific element in the file. How do you do that? Open-ended question. See how the candidate uses various types of parsers (DOM, SAX, etc) and see if he explores the usage of XPath.
You're supposed to unit test Java class that accesses a database,
in a machine that doesn't have database server installed,
and no external connectivity at all. What's your approach?
Open-ended
question. See if the answer tackles the problem effectively.
See if he/she suggests mocking as an option
How do you ensure your unit tests have covered all paths /
possibilities?
Use
code coverage tool like Cobertura, Emma, etc
How do you unit-test a private method? -
Use Reflection, set accessible property of that method to true
- Call an accessible method that calls the private method and observe the
effect caused by calling the private method

XML Message Verification in B-Puma:

1, First get the XML message from cache component, using the ConsumerTemplate.receiverBody(uri, TIMEOUT, Node.class); and cast to Element.

2, Pass the obtained Element to OrderVerifier constructor which builds a XPathVerifyDelegate(Node):

private static XPath xpath = XPathFactory.newInstance().newXPath();

String actual = xpath.evaluate(key, node); //key is like @limitPrice, the path of the data

在这里,Node是从camel consumerTemplate里直接收到的,如果你的XML在一个file里而且你想用XPath读,你需要首先读进一个DOM object里:

DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = null;
try {
    builder = builderFactory.newDocumentBuilder();
} catch (ParserConfigurationException e) {
    e.printStackTrace(); 
}
try {
    Document document = builder.parse(newFileInputStream("c:\\employees.xml"));
} catch (SAXException e) {
    e.printStackTrace();
} catch (IOException e) {
    e.printStackTrace();
}
//read an xml node using xpath
String expression = "/Employees/Employee[@emplid='3333']/email";
Node node = (Node) xPath.compile(expression).evaluate(xmlDocument, XPathConstants.NODE);

About DOM:

The parser traverses the XML file and creates the corresponding DOM objects. These DOM objects are linked together in a tree structure.

The Node object is the primary data type for the entire DOM. Document不只包括树型元素结构,所以Element这种结构只是获取Document里的树型结构Document.getDocumentElement()。Document和Element都是Node interface的sub-interface。

A node can be an element node, an attribute node, a text node, or any other of the node types explained in the "Node types" chapter. Element is the only kind of node that can have child nodes and attributes.

DOM on the other hand is more robust - it reads the entire XML document into a tree, and is very efficient when you want to alter, add, remove data in that XML tree. XPath is useful when you only need a couple of values from the XML document, and you know where to find them (you know the path of the data, /root/item/challange/text).

如果你用DOM来获取元素:

Element rootElement = document.getDocumentElement();
NodeList nodes = element.getChildNodes();

for(int i=0; i<nodes.getLength(); i++){
Node node = nodes.item(i); if(node instanceof Element){
//a child element to process
Element child = (Element) node;
String attribute = child.getAttribute("width");
}
}

A Node is a part of the DOM tree, an Element is a particular type of Node : e.g. <foo> This is Text </foo>

          (I)Node

(I)Document   (I)Element    (I)Attr

You have a foo Element, (which is also a Node, as Element inherits from Node) and a Text Node 'This is Text', that is a child of the foo Element/Node

XPath为XML Path language: XPath包含一个标准函数库

XPath语法:

nodename        选取名为nodename节点的所有子节点

/                     从根节点选取

//                    选取节点,所有,不管位置

@                    选取属性

Predicates:

例如  //tiltle[@lang]   选取所有属性lang的所有title节点

    /bookstore/book[price>35]/title                    child::*    选取当前节点的所有子元素

    //title| //price    选取若干路径"|"                     ancestor::book  选取当前节点的所有book先辈

XML Message Constructing in B-Puma:

<from uri="direct:default-order-record"/>

<transform>
  <constant>&lt;xml/&gt;</constant>
</transform>
<to uri="xquery:com/barcap/eq/baths/core/puma/templates/DefaultOrderRecord.xml"/>

then Node.setAttribute(String name, String value); 

Unit Testing or Component Testing:

1, To write a test with the JUnit 4.x/TestNG-6-3.1 framework you annotate a method with the @org.junit.Test/@org.testng.annotations.Test

annotation.

2, In TestNG, if you want to run multi-tests as a suite: define XML file with <suite> tag.

<suite><test><packages><package>...</>

Then in the test or base test, use annotation @BeforeSuite and @AfterSuite to initiate/close actors.

Other annotations: @BeforeGroups, @BeforeClass, @BeforeTest, @BeforeMethod.

3, Base class in B project:

extends AbstractTestNGSpringContextTests, part of spring-test-3.1.1, which integrates the Spring TestContext framework with testing support in a TestNG enrironment.

For example, at your base class, @ContextConfiguration(locations = {"/pApp-actors.xml"}), @ActiveProfiles({"SIT2"}).

4, Spring configuration files in test project:

====

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

<import resource="spring/puma.xml"/>

<bean id="paul" p:pumaGuiUsername="${puma.user.name}" class="com.barcap.eq.baths.puma.framework.interactor.impl.PumaActorCamel"/>

<beans profile="SIT2">
  <bean p:locations="classpath*:env/US-SIT2-*.properties" class="org.apache.camel.spring.spi.BridgePropertyPlaceholderConfigurer"/>
</beans>

====

Either to use <beans profile> in Spring xml and @ActiveProfiles annotation, to switch among profiles,

Or <profile> in Maven xml and mvn -P Profile1.

5, TestNG also include JUnit's Assert class, which lets you perform assertions on complex objects:

import static org.testng.AssertJUnit.*; assertEquals("Beust", m_lastName);

assertTrue(boolean value), assertFalse(boolean value), assertNull(object),  assertNotNull(object), assertEquals(expected, actual).

6, TestNG DataProvider

A Data Provider is a method on your class that returns an array of array of objects.  This method is annotated with @DataProvider:

@DataProvider(name = "test1")

public Object[][] createData() { return new Object[][] {{"Constance", new Integer(12)}, {"Roxy", new Integer(33)}};}

@Test(dataProvider = "test1")

public void verifyData(String name, Integer age) {System.out.println(name + " " + age);}

7, Mockito testing

The concept behind mock objects is that we want to create an object that will take the place of the real object. This mock object will expect a certain method to be called with certain parameters and when that happens, it will return an expected result.

http://www.codeproject.com/Articles/516360/Mockito-a-great-mock-framework-for-Java-developmen

这里,没有PersonDao的具体implementation,只是为了test PersonService class,所以需要@Mock PersonDao personDao; 然后stub method calls, verify interations.

public class PersonServiceTest
{
@Mock
private PersonDao personDAO;
private PersonService personService;
@Before
public void setUp()
throws Exception
{
MockitoAnnotations.initMocks( this );
personService = new PersonService( personDAO );
}
@Test
public void shouldUpdatePersonName()
{
Person person = new Person( 1, "Phillip" );
when( personDAO.fetchPerson( 1 ) ).thenReturn( person ); //Stub method calls
boolean updated = personService.update( 1, "David" );
assertTrue( updated );
verify( personDAO ).fetchPerson( 1 ); //verify interation
ArgumentCaptor<Person> personCaptor = ArgumentCaptor.forClass( Person.class );
verify( personDAO ).update( personCaptor.capture() );
Person updatedPerson = personCaptor.getValue();
assertEquals( "David", updatedPerson.getPersonName() );
// asserts that during the test, there are no other calls to the mock object.
verifyNoMoreInteractions( personDAO );
}
@Test
public void shouldNotUpdateIfPersonNotFound()
{
when( personDAO.fetchPerson( 1 ) ).thenReturn( null );
boolean updated = personService.update( 1, "David" );
assertFalse( updated );
verify( personDAO ).fetchPerson( 1 );
verifyZeroInteractions( personDAO );
verifyNoMoreInteractions( personDAO );
}
}

8, @Listeners annotation in TestNG

提供了一种改变TestNG behavior的方法,你create的listener class要implement ITestNGListener,比如在你的listener class里:

@Override
public void onConfigurationFailure(final ITestResult testResult) {
  reporter.onConfigurationFailure(testResult);
}

9,JUnit 4里有expected = IllegalArgumentException.class,在testng里是expectedExceptions = {}。

												

[Java Basics3] XML, Unit testing的更多相关文章

  1. Java Unit Testing - JUnit & TestNG

    转自https://www3.ntu.edu.sg/home/ehchua/programming/java/JavaUnitTesting.html yet another insignifican ...

  2. 10 Unit Testing and Automation Tools and Libraries Java Programmers Should Learn

    转自:https://javarevisited.blogspot.com/2018/01/10-unit-testing-and-integration-tools-for-java-program ...

  3. Unit Testing of Classes in Java

    Every class can have a main method. That is a handy trick for unit testing of classes. For example, ...

  4. C/C++ unit testing tools (39 found)---reference

    http://www.opensourcetesting.org/unit_c.php API Sanity AutoTest Description: An automatic generator ...

  5. Unit Testing of Spring MVC Controllers: “Normal” Controllers

    Original link: http://www.petrikainulainen.net/programming/spring-framework/unit-testing-of-spring-m ...

  6. Unit Testing of Spring MVC Controllers: Configuration

    Original Link: http://www.petrikainulainen.net/programming/spring-framework/unit-testing-of-spring-m ...

  7. Javascript单元测试Unit Testing之QUnit

    body{ font: 16px/1.5em 微软雅黑,arial,verdana,helvetica,sans-serif; }           QUnit是一个基于JQuery的单元测试Uni ...

  8. Unit Testing a zend-mvc application

    Unit Testing a zend-mvc application A solid unit test suite is essential for ongoing development in ...

  9. java对xml节点属性的增删改查

    学习本文之前请先看我的另一篇文章JAVA对XML节点的操作可以对XML操作有更好的了解. package vastsum; import java.io.File; import java.io.Fi ...

随机推荐

  1. iframe自动高度

    <script> //设置iframe自动高度 function setIframe(id){ var fn = function(){ try{ var iframe = typeof ...

  2. 如何使用Notepad++编译运行php

    安装编译运行文件的插件.插件-Plugin Manager-Show Plugin Manager. 找到NppExec插件,从这个插件的名字来看就是各位文件的执行.点击Install.需要下载安装, ...

  3. 三、最小化的Spring XML配置

    Spring 提供了自动装配(自动识别如何装配Bean的依赖关系)和自动检测(检测哪些类需要被配置成Spring Bean) 1.自动装配Bean的属性 1.1四种类型得自动装配:byName.byT ...

  4. windows下安装MongoDB要注意的问题

    1.  errno:10061 由于目标计算机积极拒绝,无法连接. 解决方法:在mongoDB的bin目录下,打开命令行,输入: mongod --dbpath "c:\data\db&qu ...

  5. guava学习--monitor

    转载:https://my.oschina.net/realfighter/blog/349924   https://my.oschina.net/realfighter/blog/349926 M ...

  6. JavaScript 面向对象(三) —— 高级篇

    JavaScript 面向对象(一) —— 基础篇 JavaScript 面向对象(二) —— 案例篇 一.json方式的面向对象 首先要知道,js中出现的东西都能够放到json中.关于json数据格 ...

  7. Swiper说明&&API手册 【中文手册Swiper】

     原文地址:http://www.cnblogs.com/scavengers/p/3760449.html 示例: <link rel="stylesheet" href= ...

  8. 那些年因为粗心导致的外链css无效

    css文件三种引用的三种方式: 1.外链: <link rel= "stylesheet" href=""> 注:如果使用外链式绝对不可以忘记 re ...

  9. Python之路,Day9, 进程、线程、协程篇

    本节内容 操作系统发展史介绍 进程.与线程区别 python GIL全局解释器锁 线程 语法 join 线程锁之Lock\Rlock\信号量 将线程变为守护进程 Event事件 queue队列 生产者 ...

  10. Windows下QT Creator工程中添加文件夹

    在QT项目,常常会有很多头文件和源文件,但是QT Creator中却没有添加文件夹的功能,造成项目代码混乱.   下面是建立文件的步骤: 1.打开工程目录,在目录下建立文件夹,如建立文件SerialP ...