Difference between @Mock, @MockBean and Mockito.mock()

Plain Mockito library import org.mockito.Mock; … @Mock MyService myservice; and import org.mockito.Mockito; … MyService myservice = Mockito.mock(MyService.class); come from the Mockito library and are functionally equivalent. They allow to mock a class or an interface and to record and verify behaviors on it. The way using annotation is shorter, so preferable and often preferred. Note … Read more

Java: How to test methods that call System.exit()?

Indeed, Derkeiler.com suggests: Why System.exit() ? Instead of terminating with System.exit(whateverValue), why not throw an unchecked exception? In normal use it will drift all the way out to the JVM’s last-ditch catcher and shut your script down (unless you decide to catch it somewhere along the way, which might be useful someday). In the JUnit … Read more

How does Junit @Rule work?

Rules are used to add additional functionality which applies to all tests within a test class, but in a more generic way. For instance, ExternalResource executes code before and after a test method, without having to use @Before and @After. Using an ExternalResource rather than @Before and @After gives opportunities for better code reuse; the … Read more

Assert an object is a specific type

You can use the assertThat method and the Matchers that comes with JUnit. Take a look at this link that describes a little bit about the JUnit Matchers. Example: public class BaseClass { } public class SubClass extends BaseClass { } Test: import org.junit.Test; import static org.hamcrest.CoreMatchers.instanceOf; import static org.junit.Assert.assertThat; /** * @author maba, 2012-09-13 … Read more

AssertContains on strings in jUnit

If you add in Hamcrest and JUnit4, you could do: String x = “foo bar”; Assert.assertThat(x, CoreMatchers.containsString(“foo”)); With some static imports, it looks a lot better: assertThat(x, containsString(“foo”)); The static imports needed would be: import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.CoreMatchers.containsString;

When do Java generics require

First – I have to direct you to http://www.angelikalanger.com/GenericsFAQ/JavaGenericsFAQ.html — she does an amazing job. The basic idea is that you use <T extends SomeClass> when the actual parameter can be SomeClass or any subtype of it. In your example, Map<String, Class<? extends Serializable>> expected = null; Map<String, Class<java.util.Date>> result = null; assertThat(result, is(expected)); You’re … Read more

Getting “NoSuchMethodError: org.hamcrest.Matcher.describeMismatch” when running test in IntelliJ 10.5

Make sure the hamcrest jar is higher on the import order than your JUnit jar. JUnit comes with its own org.hamcrest.Matcher class that is probably being used instead. You can also download and use the junit-dep-4.10.jar instead which is JUnit without the hamcrest classes. mockito also has the hamcrest classes in it as well, so … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)