Surefire is not picking up Junit 5 tests

The maven-surefire-plugin, as of today, does not have full support of JUnit 5. There is an open issue about adding this support in SUREFIRE-1206. As such, you need to use a custom provider. One has already been developed by the JUnit team; from the user guide, you need to add the junit-platform-surefire-provider provider and the … Read more

Python unittests in Jenkins?

sample tests: tests.py: # tests.py import random try: import unittest2 as unittest except ImportError: import unittest class SimpleTest(unittest.TestCase): @unittest.skip(“demonstrating skipping”) def test_skipped(self): self.fail(“shouldn’t happen”) def test_pass(self): self.assertEqual(10, 7 + 3) def test_fail(self): self.assertEqual(11, 7 + 3) JUnit with pytest run the tests with: py.test –junitxml results.xml tests.py results.xml: <?xml version=”1.0″ encoding=”utf-8″?> <testsuite errors=”0″ failures=”1″ name=”pytest” … Read more

Checking that a List is not empty in Hamcrest

Well there’s always assertThat(list.isEmpty(), is(false)); … but I’m guessing that’s not quite what you meant 🙂 Alternatively: assertThat((Collection)list, is(not(empty()))); empty() is a static in the Matchers class. Note the need to cast the list to Collection, thanks to Hamcrest 1.2’s wonky generics. The following imports can be used with hamcrest 1.3 import static org.hamcrest.Matchers.empty; import … Read more

java.lang.NoClassDefFoundError: com/sun/mail/util/MailLogger for JUnit test case for Java mail

The javax.mail-api artifact is only good for compiling against. You actually need to run code, so you need a complete implementation of JavaMail API. Use this: <dependency> <groupId>com.sun.mail</groupId> <artifactId>javax.mail</artifactId> <version>1.6.2</version> </dependency> NOTE: The version number will probably differ. Check the latest version here.

Mockito – NullpointerException when stubbing Method

I had this issue and my problem was that I was calling my method with any() instead of anyInt(). So I had: doAnswer(…).with(myMockObject).thisFuncTakesAnInt(any()) and I had to change it to: doAnswer(…).with(myMockObject).thisFuncTakesAnInt(anyInt()) I have no idea why that produced a NullPointerException. Maybe this will help the next poor soul.

How do I assert equality on two classes without an equals method?

There is many correct answers here, but I would like to add my version too. This is based on Assertj. import static org.assertj.core.api.Assertions.assertThat; public class TestClass { public void test() { // do the actual test assertThat(actualObject) .isEqualToComparingFieldByFieldRecursively(expectedObject); } } UPDATE: In assertj v3.13.2 this method is deprecated as pointed out by Woodz in comment. … 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

How to use ArgumentCaptor for stubbing?

Assuming the following method to test: public boolean doSomething(SomeClass arg); Mockito documentation says that you should not use captor in this way: when(someObject.doSomething(argumentCaptor.capture())).thenReturn(true); assertThat(argumentCaptor.getValue(), equalTo(expected)); Because you can just use matcher during stubbing: when(someObject.doSomething(eq(expected))).thenReturn(true); But verification is a different story. If your test needs to ensure that this method was called with a specific argument, … Read more