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. 手机版web相关meta配置

    若页面需默认用ie兼容内核,增加标签: 若页面需默认用ie标准内核,增加标签: --> 页面标题

  2. Currency System in Geraldion

    standard output A magic island Geraldion, where Gerald lives, has its own currency system. It uses b ...

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

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

  4. Java代理模式/静态代理/动态代理

    代理模式:即Proxy Pattern,常用的设计模式之一.代理模式的主要作用是为其他对象提供一种代理以控制对这个对象的访问. 代理概念 :为某个对象提供一个代理,以控制对这个对象的访问. 代理类和委 ...

  5. Python实践所遇问题记录

    1.在cmd中直接输入'python'提示:'python'不是内部或外部命令,也不是可运行的程序或批处理文件. 原因:没有为Python设置环境变量. 解法:控制面板->系统->高级系统 ...

  6. Java基本

    定义类的方法 class 类名{ 属性; 方法; } 属性也叫成员变量,主要用于描述累的状态方法也叫成员方法,主要用于描述类的行为 class Person{ int age; void show() ...

  7. 判断IE版本的HTML语句详解,如:[if lte IE 9]……[endif]

    我们常常会在网页的HTML里面看到形如[if lte IE 9]……[endif]的代码,表示的是限定某些浏览器版本才能执行的语句,那么这些判断语句的规则是什么呢?请看下文: <!--[if ! ...

  8. VBA续嘘嘘

    什么是VBA?它有什么作用? A.实现Excel中没有实现的功能. B.提高运行速度. C.编写自定义函数. D.实现自动化功能. E.通过插入窗体做小型管理软件. VBA在哪里存放的?怎么运行? A ...

  9. PDA项目介绍

    开发工具:Microsoft Visual Studio 2008 SDK:      Windows Mobile 6 SDK 数据库:     Oracle 开发语言:C#(3.5) 版本控制工具 ...

  10. React Native 打包.jsx文件

    最近在研究React Native.感觉开发效率确实不错,但jsx语法写起来感觉不怎么顺手. 试用了Sublime Text 3和Visual Studio Code写代码,感觉反应总是慢一拍. 还是 ...