I have a class that starts multiple threads which all run while(true) loops. Is it possible to have an Assert statements on the state of a test after it has run for a certain amount of time? The Timeout functionality would work, if it didn’t fail the test. This is a contrived example to show what I’m trying to do.
Tag: junit
Mocking Instance with deep stubs
I want to mock a lazy injection with deep stubs using: But myClassInstance.get().myMethod() results in a NullPointerException because myClassInstance.get() returns null instead of a mock. Is this an unsupported feature or am I doing something wrong? I’m using junit-jupiter 5.9.0 and mockito-junit-jupiter 4.6.1. Answer This is a result of erasure of generic types. Mockito does not have access to the
Spring repository : Junit test succeeds on custom delete even when @Transactional is missing
I have a Spring repository that has a method like this : When testing this method in a Junit Test, it does work as intended. However, the delete does not work on our production DB. For it to work on the production DB, we had to add the @Transactional annotation to the method in the repository This is an issue
Junit5 Parameterized Test LifeCycle. why @BeforeAll and static block run after all the tests
I am posting a simple code below, why does @BeforeAll annotated method and static block run after the Parameterized tests? How do I register a common object or Database connection in this case before loading the Parameterized test in Junit5 or leverage the functionality of @BeforeAll or static block. (FYI: The equivalent code for Parameterized test in Junit4 runs static
Mockito expecting argument mismatch
I’m writing a Unit test with JUnit 4 and Mockito 4.6.1. I’m mocking an interface method with a specific input value. When a different value is passed, I’m expecting an argument mismatch error, but it’s not being thrown. Consider the following example: I’m mocking SomeInterface.test(true) to return 1. This works as expected. Now when I call mock.test(false), I’m expecting an
How to run mock server tests in isolation?
I have these two mock server tests. When I launch these, the second test fail because the two tests are not launched in isolation. The mocking of the HTTP call in the first method isn’t override in the second method. Is there a Mock Server property or a JUnit property to run these two tests in isolation ? Answer At
Use H2 as test DB in JUnit5
I’m developing an app in Spring Boot. I use a PostreSQL DB for production, and I want my JUnit 5 tests to run on a H2 memory DB. Problem is, after some configuration, the tests still don’t seem to run on the in-memory db: I can access entities from the prod db (a saved entity doesn’t persist in the prod
I’m receiving an java.lang.AssertionError in a test using REST Assured
This is the first time for me using Gson and REST Assured, and I am really struggling to be honest. I need to verify that the content type is JSON and it is, but the test fails with the following message that you will see below. I am not writing all the imports that I have, if it is needed
Mocking an object returned from private method in java
I have a requirement where i want to mock an object that is return from private method. I am working with junit and mockito in my project. I can not paste the actual code here but sample code will look like this. Limitation here is that the code is legacy one and I can not refactor as of now Class
assertAll a list of booleans
There is a list of pages that can be accessed by a user. The verifyAccess has a function that returns true/false depending on which page the user is trying to access. For example – look for the profile IMG locator on a profile page, the Logout button on the logout page, and so on … I am using below imports