Questions tagged [junit5]
Version 5 of the popular JUnit testing framework for the JVM. JUnit is a framework for writing repeatable tests. It is an instance of the xUnit architecture for unit testing frameworks.
2,909
questions
0
votes
1
answer
17
views
JUnit Test For Sorted ArrayList
so, I have method:
public List<Map.Entry<String, Integer>> sortMap(HashMap<String, Integer> map) {
List<Map.Entry<String, Integer>> list = new ArrayList<>(...
0
votes
0
answers
17
views
Hi , can anyone help me in configuring JUnit framework for testing purposes?
I have downloaded the JAR files and added them in the project's build path under libraries. However, when I try to run the code, it's giving me this error message: java.lang.NoClassDefFoundError: org/...
1
vote
0
answers
28
views
How to test for methods in a enum class? why is my code for testing not triggering the methods mentioned?
I'm trying to mock the class and call the methods using that, but for some reason the methods are not being initiated.
The class to be tested
public enum Square {
private final int a;
...
0
votes
0
answers
16
views
PowerMock whenNew, thenReturn equivalent in Mockito and JUnit5
We are migrating from PowerMock and JUnit 4 to Mockito and JUnit5. I have a very simple setup class.
@BeforeEach
public void setUp() throws Exception {
service = new ...
0
votes
1
answer
19
views
How to pass expected and actual value in paramererized test in JUnit 5
I am trying to implement Parameterized test in which I have a set of input and expected values which I want to test using assertEquals method of JUnit. I'm using JUnit version 5.x for this I am ...
1
vote
2
answers
26
views
How to test predicate in an unit test?
I have an Apache Camel application, which uses a Choice with a Predicate. How can I test the predicate without an integration test?
Code
@SpringBootApplication
public class TestApplication {
public ...
-1
votes
0
answers
14
views
Debugging build on jenkins
The following command runs fine on my local machine.
mvn --batch-mode -f pom.xml clean install -Djacoco.skip.instrument=false dependency:tree -DoutputType=dot -DoutputFile=C:\D-drive\repositories\...
0
votes
1
answer
42
views
Unit test on a method with a instantiated custom object
How do I control the output value of the getCapturedCharacterResponse() for my junit 5 testing?
Example implementation:
class ToBeTestedClass{
private CaptureResponseWrapper co;
void ...
0
votes
1
answer
27
views
Unit Testing on Springboot WebClient with MockWebServer
After searching around the net for few days, couldn't really find the resources I need for my use case as many of it are implemented in Java.
I have a service class that is calling external api, and I ...
0
votes
0
answers
36
views
Using Spring Boot and JUnit 5, how can I write a test case for this method, filterAddressByCustomField?
I am using Spring Boot and JUnit 5. How can I write a test case for this method?
public static Predicate<Address> filterAddressByCustomField (String fieldName, String fieldValue)
return Address ...
0
votes
0
answers
14
views
Unit testing errors with @EmbeddedKafka and spring boot
I have IdentManagerApp with regular SpringBoot 2.6.8 and Kafka producers and consumers. Additionally I have custom Kafka configurations and failure scenarios handling with Retry/DLT topics. The ...
0
votes
0
answers
19
views
How to get thread executing test in gradle?
When we run test using Junit in Gradle, we can get information like start time and end time using result.getStartTime().
But how to get the thread name and id of thread executing the test for each ...
0
votes
0
answers
26
views
Implementation of AspectJ example not working
I'm try to implement a simple AspectJ annotation example from https://www.baeldung.com/aspectj. The difference is, that I want to use the annotation at a JUnit TestClass. I googled a while for the ...
0
votes
0
answers
13
views
Junit5 @MethodSource passing parameter to the method
I'm looking for how to pass a parameter into a metod that use the @MethodSource annotatio in Junit5
For example I need to invoke @MethodSource annotation and passing a value into the method "...
0
votes
0
answers
33
views
How to mock new instance of class in JUnit 5
I am trying to mock the new instance of CourseLoader in the method I'm testing so when I call loadAll() it returns my mock.
I have setup a mock() in the unit test but it seems to be ignoring what I'm ...
1
vote
1
answer
93
views
When is jUnit4 going to be deprecated?
Since junit5 out there, how long jUnit4 going to be supported? Any plans for deprecation, when is it going to be? We are just trying to gauge whether we need to migrate existing jUnit4 test cases to ...
0
votes
0
answers
11
views
Nullpointer Exception on Jenkins
I am getting an error on my Jenkins build, which I checked in the logs is using the command
[![enter image description here][1]][1]
It shows Tests run: 211, Failures: 1
But when I run the same maven ...
0
votes
0
answers
33
views
java.lang.ClassFormatError: Class file version does not support constant tag 18 in class file com/test/main/TestClass
I came accross this error java.lang.ClassFormatError: Class file version does not support constant tag 18 in class file com/test/main/MathUtilsTest while compiling Junit Test Cases. Please find below ...
0
votes
0
answers
26
views
How to migrate Rules to JUnit 5
How can I migrate rules to JUnit 5
@Rule
public WireMockRule wireMockRule = new WireMockRule(options().port(8888)
.usingFilesUnderClasspath("src/dev/resources"));
@Rule
...
0
votes
1
answer
14
views
Springboot Junit At least one SecurityBuilder<? extends SecurityFilterChain> needs to be specified error
Springboot 2.5.13 (with security on), Junit5:
Error: At least one SecurityBuilder<? extends SecurityFilterChain> needs to be specified. Typically this is done by exposing a SecurityFilterChain ...
0
votes
0
answers
20
views
Why is MockBean not working in an integrationtest with RabbitMQ?
I have a Spring Boot application with RabbitMQ and I try to test my code in an integration test. My integration test does work in some cases, but not in all cases.
Application code
@Component
@...
1
vote
0
answers
46
views
JUnit 5: react on Exception from Extension (Callback)
In my Tests I have several Extensions via the *Callback Implementation.
(BeforeAllCallback, BeforeEachCallback, ....)
And I want to react on specific Exceptions from the implementation of the ...
0
votes
2
answers
53
views
Failed to release mocks - Mockito
I am trying to upgrade from Mockito version 1.0.19 to 4.0.0 and using Junit 5, since I am unable to mock static in older version of mockito. I am getting "Failed to Release mocks" error..
...
0
votes
1
answer
16
views
Wiremock - Connection Refused - JUnit5
I have a stub that I can see under /__admin/ but when making any sort of rest calls nothing registers in either __admin/requests or __admin/requests/unmatched which I feel is strange. Is Wiremock ...
0
votes
1
answer
20
views
BlockHound allowBlocking not working with junit5
I am using blockhound to detect blocking calls, I would like to allow my custom methods but its not working and still throwing error.
i am using junit5 with blockhound-junit-platform
@BeforeAll
...
0
votes
1
answer
23
views
Issue with Mockito when mocking a method in Java Interface
I'm having issues when mocking a method in Java interface using Junit5 and Mockito 3.8. The test is failing with the reason:
Wanted but not invoked:
remoteCommands.getServiceStatus("ABC");
-&...
1
vote
1
answer
28
views
Mock and throw error for File readAllBytes
Want to mock File.readAllBytes to throw error first and then bytes[]..
getQueryTextFromFile is a private method and calling from another method to get bytes[]
private byte[] getQueryTextFromFile() {
...
0
votes
1
answer
19
views
Mockito exectues "when-then" from previous test
When I run these tests separetly, they work just fine but when I execute "run all tests in class", then test shouldSaveProduct fails. I get exception("DataIntegrityViolationException&...
0
votes
0
answers
19
views
Migrating Rules from Junit4 to Junit5
I have some Junit4 tests. I want to migrate the tests to use Junit5, but there are somethings that I can't really understand how I can change it to Junit5. I am using @Rule annotations and in some ...
0
votes
0
answers
32
views
How to write unit testcases for springboot schedulers which is using fixedDelay
I have scheduled job using spring schedulers. Need help to write UnitTestcsaes for this.
On mocking and calling scheduler method how to handle this waiting time.. Do we really need to wait for such ...
-1
votes
0
answers
27
views
How to write junit for void method call of same class in junit?
How to cover below m2() method? In pit report mutant is still there. I searched it is suggest me to use spy for this kind of problem, but I don't know which case it is using.
Class Data{
void M1(){
...
2
votes
1
answer
31
views
Subsequent calls to JPA failing in SpringBoot
I'm trying to execute tests but only the first call to findBy() is successful. Running with Spring Boot v2.6.7, Spring v5.3.19, hibernate jar = 5.6.8.
Application application = Application.builder()...
0
votes
1
answer
53
views
Getting InvalidUseOfMatchingException or NullPointerException when all test run together
Getting following exceptions when run all tests together but works as expected when executed individually
org.mockito.exceptions.misusing.InvalidUseOfMatchersException:
Misplaced or misused argument ...
3
votes
1
answer
43
views
Spring MVC Controller testing PUT
Trying to test my web tier (spring boot, spring mvc) with junit5 and mockito. All other tests on http methods (get, put, ...) are working fine but the update.
Following the code.
Controller:
@...
0
votes
1
answer
29
views
How To Test Login Method In JUnit?
I have been using white box testing with JUnit to test some methods that I have in my DAO Implementation class. I am trying to test the login functionality, which is defined in the Service ...
1
vote
0
answers
41
views
New code is not showing in Sonarqube Coverage on new code
I am trying to cover few lines of the code with junit test case. Though the test case is covering the lines which I found after debugging the test case, but in Sonarqube Coverage on new code those ...
-1
votes
0
answers
37
views
how to write junit test cases for if condition throws exception in java using mockito
if(!manifestFile.contains(TDW_RPM_STATEMENTS)) {
throw new FatalBatchException(ManifestService.class, MANIFEST_INVALID_FILE_NAME_ERROR, MANIFEST_FILE_NAME_VALIDATION_ERROR_CODE);
}
1
vote
0
answers
22
views
How to test custom auto-configurated beans of @RequestScope on Spring Boot?
I am creating a custom auto-configurated[1] "starter"[2] to create "request" scoped beans for Unleash in Spring Boot 2.6. I would like to write some JUnit 5 tests to verify is the ...
0
votes
2
answers
36
views
How to mock Function interface from java 8 and System class
I want to mock below call :
((Function<String, String>) System::getenv).apply(TransformerConstants.TRANSFORMER_POOL_SIZE)
I tried using below code :
PowerMockito.when(((Function<String, ...
0
votes
1
answer
26
views
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 ...
0
votes
0
answers
37
views
user repository is not Mocking in spring boot junit
I want to test one of my UserService methods with JUnit in SpringBoot
here is my UserService class:
import io.project.exception.NotFoundException;
import io.project.logger.CrudOperationLogger;
import ...
1
vote
1
answer
29
views
How do I convert this test rule to JUnit5?
I have some custom rule using junit4 which I would like to convert to junit5. However I'm not able to find good documentation on migrating a MethodRule implementation other than that I should be using ...
0
votes
0
answers
21
views
Write JUnit test output to log4j2 logfile
The question I had, when extracting my google-foo was if I can redirect the output of a JUnit test to a dedicated log4j2 logfile.
The configuration of the logfile was the easy step:
@BeforeAll
public ...
0
votes
0
answers
23
views
Wiremock test running individually, but not when executing all tests
I created some integration tests using wiremock. They pass individually, but when I execute the test task from gradle they fail with the following error:
2022-05-09 11:42:40.077 ERROR 23116 --- [...
1
vote
1
answer
30
views
Launching Activity with extra attached to Intent in Instrumented test, running on espresso and JUnit 5
I'd like to launch my ActivityScenarioExtension with an extra attached to the startActivityIntent. The extra-less scenario extension launching, which works just as intended, I carry out via
internal ...
-2
votes
1
answer
31
views
JUnit5 test with bad data type
In order to learn JUnit test cases
I want to write a simple JUnit5 test case and i get
an error
My class
1 easy class
public class Med {
private String name;
private double price;
...
1
vote
2
answers
113
views
Gradle problems of adding koin test dependencies
I'm a beginner with gradle and would like to use koin in my Kotlin project.
However, I get the following error
Execution failed for task ':compileTestKotlin'.
> Error while evaluating property '...
0
votes
0
answers
31
views
Java.lang.NoClassDefFoundError: junit/framework/TestCase
After upgrading from JUnit 4 to JUnit 5 getting this error when i am trying to run test as JUnit in eclipse.
Getting warning: failed to load class 'org.eclipse.jdt.internal.junit.runner.junit3....
1
vote
1
answer
166
views
"message":"Head \"https://registry-1.docker.io/v2/testcontainers/ryuk/manifests/0.3.0\": unauthorized: incorrect username or password" when run test
I have added testcontainer in spring boot app
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
@SpringBootTest
@Testcontainers
@AutoConfigureMockMvc
class ...
0
votes
2
answers
48
views
How to Trigger a ClassCastException? I'm trying to unit test a class and there is this try/catch segment which i'm not able to check [closed]
public int compare(Cat cat1, Cat cat2) {
try {
Cat obj1 = cat1;
Cat obj2 = cat2;
return obj1.getCode().compareTo(obj2.getCode());
//compareTo ...