Java

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

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 !

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 »

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 »

Call Java compiler programatically

You don’t need to add any dependency private static void doCompile(File sourceDir) throws SomeException { System.out.println(“Compiling started”); JavaCompiler compiler = ToolProvider.getSystemJavaCompiler(); DiagnosticCollector diagnostics = new DiagnosticCollector(); StandardJavaFileManager fileManager = compiler.getStandardFileManager(diagnostics, null, null); Collection files = FileUtils.listFiles(sourceDir, new String[] { “java” }, true); Iterable compilationUnits = fileManager.getJavaFileObjectsFromFiles(files); List compilerOptions = Arrays.asList(“-source”, “1.6”, “-target”, “1.6”); JavaCompiler.CompilationTask task …

Call Java compiler programatically Read More »

Tomcat gets stuck during start up?

It takes more than 5 minutes for your tomcat to startup.  You think it’s your application’s program. But when you download a clean Tomcat (of the same version) can run it without dropping in any of your applications, it still gets stuck.  It may be caused by this: https://wiki.apache.org/tomcat/HowTo/FasterStartUp#Entropy_Source According to my just wasted 2 …

Tomcat gets stuck during start up? Read More »

Propagate data change from RDBMS to Solr

Basically, you will use solr’s DataImportHandler to do the importing. Hit http://solr-server:port/solr/db/dataimport?command=full-import to do a full import. Hit http://solr-server:port/solr/dataimport?command=delta-import to do a delta import So, what’s the delta? This is configured in some data-config.xml: deltaQuery=”select id from item where last_modified > ‘${dih.last_index_time}'” So it requires the table on your database must have a column called …

Propagate data change from RDBMS to Solr Read More »

G1 v.s. CMS

G1 also aims at "low pause", and it can work well on large heaps(over 4G) .   CMS, on the other hand, are not designed for large heaps. And CMS requires more configuration.