JUnit parameterized tests: how do I run only 1 specific test from IntelliJ/Eclipse?

I just tested this in Eclipse with a simple parameterized test that always fails on test #4. One is able to right-click on the failed test and select Run. Only that test then executes. Result: Frustratingly, I can’t see what Eclipse did to solve the problem. Nothing is apparently altered in the run configuration. In … Read more

How to share JUnit BeforeClass logic among multiple test classes

A solution to the first issue is to move the logic into an extension of org.junit.rules.ExternalResource hooked up to the test via a @ClassRule, introduced in JUnit 4.9: public class MyTest { @ClassRule public static final TestResources res = new TestResources(); @Test public void testFoo() { // test logic here } } public class TestResources … Read more

can’t find run as junit test in eclipse

Make sure your eclipse environment is using JUnit 4. JUnit 3 doesn’t make use of annotations (it uses the old extends TestCase style) There are few things to double check: Window > Preferences > Java > JUnit Are you seeing junit4 or junit3 imports? If that looks good, make sure the project itself is using … Read more

java.util.MissingResourceException: Can’t find bundle for base name javax.servlet.LocalStrings, locale es_ES

Caused by: java.util.MissingResourceException: Can’t find bundle for base name javax.servlet.LocalStrings, locale es_ES That’s the real error. Your running tests are missing the servlet-api dependency. If you’re using maven make sure this dependency is in your project: <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>3.1.0</version> </dependency>

Mockito mock objects returns null

It really depends on GeneralConfigService#getInstance() implementation. Also you can simplify your test code a lot if you use @InjectMocks annotation. When using MockitoJUnitRunner you don’t need to initialize mocks and inject your dependencies manually: @RunWith(MockitoJUnitRunner.class) public class GeneralConfigServiceImplTest { @InjectMocks private GeneralConfigService generalConfigService; @Mock private GeneralConfigDAO generalConfigDAO; @Test public void testAddGeneralConfigCallDAOSuccess() { // generalConfigService is … Read more

Assume vs assert in JUnit tests

As you already know, you use assert to fail a test if something goes wrong. You use assume if you have circumstances under which a test should not run. “Not run” means that it cannot fail, because, well, it did not run. So, in a hypothetical scenario where: you have different builds for different customers, … Read more

Mockito NotaMockException

As you noted, act is not a mock, and therefore you cannot record behavior on it. You could use Mockito.spy to, well, spy (or partially mock) the act object so that you only record the behavior of secondMethod and execute the actual code for firstMethod. Note, however, that matchers can’t be used in doReturn calls … Read more

Access to h2 web console while running junit test in a Spring application

As this is probably going to be a test-debugging feature, you can add it at runtime with your @Before: import org.h2.tools.Server; /* Initialization logic here */ @BeforeAll public void initTest() throws SQLException { Server.createWebServer(“-web”, “-webAllowOthers”, “-webPort”, “8082”) .start(); } And then connect to http://localhost:8082/ Note: unless you need this to run as part of your … Read more

What is the best practice to determine the execution time of the business relevant code of my junit?

in a unit test I would prefer to add a timeout to the Test with a JUnit 4 annotation, to determine whether the test passes (fast enough) or not: @Test(timeout=100)//let the test fail after 100 MilliSeconds public void infinity() { while(true); } To determine the exact Runtime of your business logic I would add the … Read more

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