JUnit Testing Exceptions [duplicate]

@Test(expected = Exception.class) Tells Junit that exception is the expected result so test will be passed (marked as green) when exception is thrown. For @Test Junit will consider test as failed if exception is thrown, provided it’s an unchecked exception. If the exception is checked it won’t compile and you will need to use other … Read more

How to mock method e in Log

This worked out for me. I’m only using JUnit and I was able to mock up the Log class without any third party lib very easy. Just create a file Log.java inside app/src/test/java/android/util with contents: package android.util; public class Log { public static int d(String tag, String msg) { System.out.println(“DEBUG: ” + tag + “: … Read more

How to intercept SLF4J (with logback) logging via a JUnit test?

The Slf4j API doesn’t provide such a way but Logback provides a simple solution. You can use ListAppender : a whitebox logback appender where log entries are added in a public List field that we could use to make our assertions. Here is a simple example. Foo class : import org.slf4j.Logger; import org.slf4j.LoggerFactory; public class … Read more

differences between 2 JUnit Assert classes

The old method (of JUnit 3) was to mark the test-classes by extending junit.framework.TestCase. That inherited junit.framework.Assert itself and your test class gained the ability to call the assert methods this way. Since version 4 of JUnit, the framework uses Annotations for marking tests. So you no longer need to extend TestCase. But that means, … Read more

Spring Boot Microservices – Spring Security – ServiceTest and ControllerTest for JUnit throwing java.lang.StackOverflowError

The error is most likely caused by declaring the AuthenticationManager as a @Bean. Try this in your test class: @MockBean private AuthenticationManager _authenticationManager; That said, the Spring Security team does not recommend exposing the AuthenticationManager in this way, see the comment in Spring issue #29215

Creating temporary database that works across maven test phases?

For this case I have created the derby-maven-plugin. It’s available from Maven Central, so you don’t need to add any extra repositories or anything. You could use it like this: <project …> <build> <plugins> <plugin> <groupId>org.carlspring.maven</groupId> <artifactId>derby-maven-plugin</artifactId> <version>1.8</version> <configuration> <basedir>${project.build.directory}/derby</basedir> <port>1527</port> </configuration> <executions> <execution> <id>start-derby</id> <phase>pre-integration-test</phase> <goals> <goal>start</goal> </goals> </execution> <execution> <id>stop-derby</id> <phase>post-integration-test</phase> <goals> <goal>stop</goal> … Read more