Assert equals between 2 Lists in Junit

For junit4! This question deserves a new answer written for junit5. I realise this answer is written a couple years after the question, probably this feature wasn’t around then. But now, it’s easy to just do this: @Test public void test_array_pass() { List<String> actual = Arrays.asList(“fee”, “fi”, “foe”); List<String> expected = Arrays.asList(“fee”, “fi”, “foe”); assertThat(actual, … Read more

Mock a constructor with parameter

The code you posted works for me with the latest version of Mockito and Powermockito. Maybe you haven’t prepared A? Try this: A.java public class A { private final String test; public A(String test) { this.test = test; } public String check() { return “checked ” + this.test; } } MockA.java import static org.hamcrest.MatcherAssert.assertThat; import … Read more

Testing Private method using mockito

Not possible through mockito. From their wiki Why Mockito doesn’t mock private methods? Firstly, we are not dogmatic about mocking private methods. We just don’t care about private methods because from the standpoint of testing private methods don’t exist. Here are a couple of reasons Mockito doesn’t mock private methods: It requires hacking of classloaders … Read more

Run single test from a JUnit class using command-line

You can make a custom, barebones JUnit runner fairly easily. Here’s one that will run a single test method in the form com.package.TestClass#methodName: import org.junit.runner.JUnitCore; import org.junit.runner.Request; import org.junit.runner.Result; public class SingleJUnitTestRunner { public static void main(String… args) throws ClassNotFoundException { String[] classAndMethod = args[0].split(“#”); Request request = Request.method(Class.forName(classAndMethod[0]), classAndMethod[1]); Result result = new JUnitCore().run(request); … Read more

Embedded MongoDB when running integration tests

I have found Embedded MongoDB library which looks quite promising and does what you have asked for. Currently supports MongoDB versions: 1.6.5 to 3.1.6, provided the binaries are still available from the configured mirror. Here is short example of use, which I have just tried and it works perfectly: public class EmbeddedMongoTest { private static … Read more

What is the JUnit XML format specification that Hudson supports?

I did a similar thing a few months ago, and it turned out this simple format was enough for Hudson to accept it as a test protocol: <testsuite tests=”3″> <testcase classname=”foo1″ name=”ASuccessfulTest”/> <testcase classname=”foo2″ name=”AnotherSuccessfulTest”/> <testcase classname=”foo3″ name=”AFailingTest”> <failure type=”NotEnoughFoo”> details about failure </failure> </testcase> </testsuite> This question has answers with more details: Spec. for … Read more

Before and After Suite execution hook in jUnit 4.x

Yes, it is possible to reliably run set up and tear down methods before and after any tests in a test suite. Let me demonstrate in code: package com.test; import org.junit.AfterClass; import org.junit.BeforeClass; import org.junit.runner.RunWith; import org.junit.runners.Suite; import org.junit.runners.Suite.SuiteClasses; @RunWith(Suite.class) @SuiteClasses({Test1.class, Test2.class}) public class TestSuite { @BeforeClass public static void setUp() { System.out.println(“setting up”); } … Read more

Initialising mock objects – MockIto

For the mocks initialization, using the runner or the MockitoAnnotations.initMocks are strictly equivalent solutions. From the javadoc of the MockitoJUnitRunner : JUnit 4.5 runner initializes mocks annotated with Mock, so that explicit usage of MockitoAnnotations.initMocks(Object) is not necessary. Mocks are initialized before each test method. The first solution (with the MockitoAnnotations.initMocks) could be used when … Read more