Why should I use Hamcrest-Matcher and assertThat() instead of traditional assertXXX()-Methods

There’s no big advantage for those cases where an assertFoo exists that exactly matches your intent. In those cases they behave almost the same. But when you come to checks that are somewhat more complex, then the advantage becomes more visible: val foo = List.of(“someValue”); assertTrue(foo.contains(“someValue”) && foo.contains(“anotherValue”)); Expected: is <true> but: was <false> vs. … Read more

Spring Test & Security: How to mock authentication?

Seaching for answer I couldn’t find any to be easy and flexible at the same time, then I found the Spring Security Reference and I realized there are near to perfect solutions. AOP solutions often are the greatest ones for testing, and Spring provides it with @WithMockUser, @WithUserDetails and @WithSecurityContext, in this artifact: <dependency> <groupId>org.springframework.security</groupId> … Read more

Python unit test with base and sub class

Do not use multiple inheritance, it will bite you later. Instead you can just move your base class into the separate module or wrap it with the blank class: class BaseTestCases: class BaseTest(unittest.TestCase): def testCommon(self): print(‘Calling BaseTest:testCommon’) value = 5 self.assertEqual(value, 5) class SubTest1(BaseTestCases.BaseTest): def testSub1(self): print(‘Calling SubTest1:testSub1’) sub = 3 self.assertEqual(sub, 3) class SubTest2(BaseTestCases.BaseTest): … Read more

How do I disable a test using pytest?

Pytest has the skip and skipif decorators, similar to the Python unittest module (which uses skip and skipIf), which can be found in the documentation here. Examples from the link can be found here: @pytest.mark.skip(reason=”no way of currently testing this”) def test_the_unknown(): … import sys @pytest.mark.skipif(sys.version_info < (3,3), reason=”requires python3.3″) def test_function(): … The first … Read more

How to test code dependent on environment variables using JUnit?

The library System Lambda has a method withEnvironmentVariable for setting environment variables. import static com.github.stefanbirkner.systemlambda.SystemLambda.*; public void EnvironmentVariablesTest { @Test public void setEnvironmentVariable() { String value = withEnvironmentVariable(“name”, “value”) .execute(() -> System.getenv(“name”)); assertEquals(“value”, value); } } For Java 5 to 7 the library System Rules has a JUnit rule called EnvironmentVariables. import org.junit.contrib.java.lang.system.EnvironmentVariables; public class … Read more

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