Month: November 2016

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 !