Populating Spring @Value during Unit Test

If possible I would try to write those test without Spring Context. If you create this class in your test without spring, then you have full control over its fields. To set the @value field you can use Springs ReflectionTestUtils – it has a method setField to set private fields. @see JavaDoc: ReflectionTestUtils.setField(java.lang.Object, java.lang.String, java.lang.Object)

How to capture a list of specific type with mockito

The nested generics-problem can be avoided with the @Captor annotation: public class Test{ @Mock private Service service; @Captor private ArgumentCaptor<ArrayList<SomeType>> captor; @Before public void init(){ MockitoAnnotations.initMocks(this); } @Test public void shouldDoStuffWithListValues() { //… verify(service).doStuff(captor.capture())); } }

How to run JUnit test cases from the command line

For JUnit 5.x it’s: java -jar junit-platform-console-standalone-<version>.jar <Options> Find a brief summary at https://stackoverflow.com/a/52373592/1431016 and full details at https://junit.org/junit5/docs/current/user-guide/#running-tests-console-launcher For JUnit 4.X it’s really: java -cp .:/usr/share/java/junit.jar org.junit.runner.JUnitCore [test class name] But if you are using JUnit 3.X note the class name is different: java -cp .:/usr/share/java/junit.jar junit.textui.TestRunner [test class name] You might need to … Read more

JUnit 5: How to assert an exception is thrown?

You can use assertThrows(), which allows you to test multiple exceptions within the same test. With support for lambdas in Java 8, this is the canonical way to test for exceptions in JUnit. Per the JUnit docs: import static org.junit.jupiter.api.Assertions.assertThrows; @Test void exceptionTesting() { MyException thrown = assertThrows( MyException.class, () -> myObject.doThing(), “Expected doThing() to … Read more

JUnit test for System.out.println()

using ByteArrayOutputStream and System.setXXX is simple: private final ByteArrayOutputStream outContent = new ByteArrayOutputStream(); private final ByteArrayOutputStream errContent = new ByteArrayOutputStream(); private final PrintStream originalOut = System.out; private final PrintStream originalErr = System.err; @Before public void setUpStreams() { System.setOut(new PrintStream(outContent)); System.setErr(new PrintStream(errContent)); } @After public void restoreStreams() { System.setOut(originalOut); System.setErr(originalErr); } sample test cases: @Test public … Read more

Mockito : how to verify method was called on an object created within a method?

Dependency Injection If you inject the Bar instance, or a factory that is used for creating the Bar instance (or one of the other 483 ways of doing this), you’d have the access necessary to do perform the test. Factory Example: Given a Foo class written like this: public class Foo { private BarFactory barFactory; … Read more

Why doesn’t JUnit provide assertNotEquals methods?

I’d suggest you use the newer assertThat() style asserts, which can easily describe all kinds of negations and automatically build a description of what you expected and what you got if the assertion fails: assertThat(objectUnderTest, is(not(someOtherObject))); assertThat(objectUnderTest, not(someOtherObject)); assertThat(objectUnderTest, not(equalTo(someOtherObject))); All three options are equivalent, choose the one you find most readable. To use the … Read more

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