There are two Application Contexts in Spring MVC apps

The root context, normally seen as biz-layer context, is normally defined as "applicationContext.xml" and loaded by ContextLoaderListener.  The other context, the web-layer one is normally defined as "mvc-servlet.xml" and loaded by DispatcherServlet. The web-layer context  will inherit beans from the root one, Except  some processors such as PropertyConfigurator. As a result, you should define two …

There are two Application Contexts in Spring MVC apps Read More »

Regex + Javascript: pitfalls concerning dot, backslash and others

console.log(“====== ^ and $ =====”); console.log(/a/.test(“abc”)); //true console.log(/^a$/.test(“abc”)); //false. Add ^ ad $ to confirm a non-partial match console.log(“====== dot =====”); console.log(/^.$/.test(“a”)); //true. A dot means any character console.log(/^\.$/.test(“a”)); //false. \. means dot console.log(/^\.$/.test(“.”)); //true. \. means dot console.log(/^[.]$/.test(“a”)); //false. A dot in brackets means a real dot console.log(/^[.]$/.test(“.”)); //true. A dot in brackets means …

Regex + Javascript: pitfalls concerning dot, backslash and others Read More »

Wildfly wierd feature in 8.2.0 — automatic redirect if your path matches a directory under your webapp

1. If there is a directory "abc" under your webapp root directory and there is no index.jsp under this "abc" directory 2. and you request http://xxx/abc regardless of GET or POST method 3. then wildfly will redirect you to: 302, GET http://xxx/abc/ The code is on github String remaining = match.getRemaining() == null ? match.getMatched() …

Wildfly wierd feature in 8.2.0 — automatic redirect if your path matches a directory under your webapp Read More »

Spring + TestNG + @Spy Example

@ContextConfiguration({“/spring/applicationContext.xml”}) public class SprintTestngWithSpyAnnotationITCase extends AbstractTestNGSpringContextTests { //this bean will take mocked objects injected into it, but it itself is not going to be mocked @InjectMocks @Resource SomeService someService; //it equals to one that is created via spy(applicationContext.getBean(SomeDAO.class)) and it will be // injected to “someService” above @Spy @Resource SomeDAO someDAO; @BeforeMethod public void beforeMethod() …

Spring + TestNG + @Spy Example Read More »

TestNg: When will @BeforeClass, @BeforeMethod, @BeforeTest be run and in what sequence?

public class KentNg { @DataProvider(name = “someData”) public Object[][] someData() { return new Object[][]{ {“a1”, “b1”}, {“a2”, “b2”} }; } @BeforeTest public void setupBeforeTest() { System.out.println(“setupBeforeTest() is run”); } @BeforeClass public void setupBeforeClass() { System.out.println(“setupBeforeClass() is run”); } @BeforeMethod public void setupBeforeMethod() { System.out.println(“setupBeforeMethod() is run”); } @Test(dataProvider = “someData”) public void foo(String a, String …

TestNg: When will @BeforeClass, @BeforeMethod, @BeforeTest be run and in what sequence? Read More »

Mockito.spy() will not work with cglib-enhanced objects

Mockito.spy() will not work with cglib-enhanced objects , even if you use doReturn() instead of thenReturn(). This can cause problems in spring + @Transactional + cblig situations. As a result, you can’t simply use @Transactional in your code, but use TransactionTemplate, or create one interface/one implementation for every class that maybe mocked in integration tests

For integration testing’s sake, use one interface + one implemenation

With mocking technology today, you may dismiss the idea of ‘one interface + one implementation’ paradigm. In fact, you may still appreciate this way of code organization. Let me ask you a question: how to test a non-public method ? For unit tests, you can set the method as package-private, and put your unit test …

For integration testing’s sake, use one interface + one implemenation Read More »

Collect an inner method’s arguments and returned values with Mockito in integration tests

Imagine you have these two classes: public static class Manager { private Worker Worker = new Worker(); public void dailyWork() { System.out.println(“Good morning”); String msg = RandomStringUtils.randomAlphabetic(10); Worker.echoTwice(msg); System.out.println(“See you tomorrow”); } public void setWorker(Worker Worker) { this.Worker = Worker; } public Worker getWorker() { return Worker; } } public static class Worker { public …

Collect an inner method’s arguments and returned values with Mockito in integration tests Read More »

Mockito.spy() should be used with doReturn() instead of thenReturn()

Let me just copy the official documentation List list = new LinkedList(); List spy = spy(list); //Impossible: real method is called so spy.get(0) throws IndexOutOfBoundsException (the list is yet empty) when(spy.get(0)).thenReturn(“foo”); //You have to use doReturn() for stubbing doReturn(“foo”).when(spy).get(0); Don’t ask why !