I have one parametrized Junit test in my class. If I initialize all objects used in this test in @Before method they are not accessible in this parametrized Junit test and it throws NUllPointer Exception, due to which I have to initialize all theses objects again in parametrized Junit test. Why is this behavior ?
This is my parametrized Junit test where I have initialize all objects again and even I need to mock them also again.
@ParameterizedTest
@CsvSource({"1,5550,true","1,0,false","0,5550,false"})
public void itemsAvailableValidatorTest(int kioskId, int siteNbr,Boolean expected) throws Exception {
ItemAvailableRequest request = new ItemAvailableRequest();
ItemAvailableRequestValidator itemAvailableRequestValidator = new ItemAvailableRequestValidator();
context = Mockito.mock(ConstraintValidatorContext.class);
builder = Mockito.mock(ConstraintValidatorContext.ConstraintViolationBuilder.class);
Mockito.when(context.buildConstraintViolationWithTemplate(Mockito.anyString()))
.thenReturn(builder);
Mockito.when(context.buildConstraintViolationWithTemplate(Mockito.anyString()).addConstraintViolation())
.thenReturn(context);
// set ndc and famId
List<String> ndcs = new ArrayList<>();
List<Integer> famIds = new ArrayList<>();
ndcs.add("1234");
famIds.add(1234);
// set goodRequest fields
request.setNdc(ndcs);
request.setMdsFamId(famIds);
request.setKioskId(kioskId);
request.setQty(1);
request.setSiteNbr(siteNbr);
request.setRxFillId(null); // call the custom validator method and verify the response
assertEquals(expected, itemAvailableRequestValidator.isValid(request, context));
}
If I initialize the Following objects of this test in @Before method or at class level this test fails and throughs NullPointer Exception
ItemAvailableRequest request = new ItemAvailableRequest();
ItemAvailableRequestValidator itemAvailableRequestValidator = new ItemAvailableRequestValidator();
context = Mockito.mock(ConstraintValidatorContext.class);
builder = Mockito.mock(ConstraintValidatorContext.ConstraintViolationBuilder.class);
Mockito.when(context.buildConstraintViolationWithTemplate(Mockito.anyString()))
.thenReturn(builder);
Mockito.when(context.buildConstraintViolationWithTemplate(Mockito.anyString()).addConstraintViolation())
.thenReturn(context);
Why the objects initialized in Before method or at class level are not accessible in @parametrized JunitTest ? and why they need to be specifically initialized inside @paramterized Junit test curly brackets.
Here is the code for whole class
package com.walmart.rxkioskinventory.validator;
import com.walmart.rxkioskinventory.model.request.ItemAvailableRequest;
import com.walmart.rxkioskinventory.model.validator.ItemAvailableRequestValidator;
import org.junit.Before;
import org.junit.Test;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.MockitoAnnotations;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BeanPropertyBindingResult;
import org.springframework.validation.Errors;
import javax.validation.ConstraintValidatorContext;
import java.util.ArrayList;
import java.util.List;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
public class ItemAvailableRequestValidatorTest {
@InjectMocks
ItemAvailableRequestValidator itemAvailableRequestValidator;
private ItemAvailableRequest request;
private ConstraintValidatorContext context;
private ConstraintValidatorContext.ConstraintViolationBuilder builder;
/*
good request initializer
*/
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
// mock the context
context = Mockito.mock(ConstraintValidatorContext.class);
context = Mockito.mock(ConstraintValidatorContext.class);
builder = Mockito.mock(ConstraintValidatorContext.ConstraintViolationBuilder.class);
// context.buildConstraintViolationWithTemplate returns
// ConstraintValidatorContext.ConstraintViolationBuilder
// so we mock that too as you will be calling one of it's methods
builder = Mockito.mock(ConstraintValidatorContext.ConstraintViolationBuilder.class);
// when the context.buildConstraintViolationWithTemplate is called,
// the mock should return the builder.
Mockito.when(context.buildConstraintViolationWithTemplate(Mockito.anyString()))
.thenReturn(builder);
Mockito.when(context.buildConstraintViolationWithTemplate(Mockito.anyString()).addConstraintViolation())
.thenReturn(context);
request = new ItemAvailableRequest();
// set ndc and famId
List<String> ndcs = new ArrayList<>();
List<Integer> famIds = new ArrayList<>();
ndcs.add("1234");
famIds.add(1234);
// set goodRequest fields
request.setNdc(ndcs);
request.setMdsFamId(famIds);
// error for binding the request
Errors errors = new BeanPropertyBindingResult(request, "request");
}
/*
test to verify the valid request with all valid request params
*/
@Test
public void itemsAvailableValidatorSuccessTest() throws Exception {
ItemAvailableRequest request = new ItemAvailableRequest();
// set ndc and famId
List<String> ndcs = new ArrayList<>();
List<Integer> famIds = new ArrayList<>();
ndcs.add("1234");
famIds.add(1234);
// set goodRequest fields
request.setNdc(ndcs);
request.setMdsFamId(famIds);
request.setKioskId(1);
request.setQty(1);
request.setSiteNbr(5550);
request.setRxFillId(1);
// call the custom validator method and verify the response
assertEquals(true, itemAvailableRequestValidator.isValid(request, context));
}
/**
*Test to verify response when request is null
*/
@Test
public void itemsAvailableValidatorNullRequestTest() {
ItemAvailableRequest request = null;
// call the custom validator method and verify the response
assertEquals(false, itemAvailableRequestValidator.isValid(request, context));
}
/**
*Test to verify response when quantity is invalid.
*/
@Test
public void itemsAvailableValidatorInvalidQuantityTest() {
ItemAvailableRequest request = new ItemAvailableRequest();
// set ndc and famId
List<String> ndcs = new ArrayList<>();
List<Integer> famIds = null;
ndcs.add("1234");
// set goodRequest fields
request.setNdc(ndcs);
request.setMdsFamId(famIds);
request.setKioskId(1);
request.setQty(0.5);
request.setSiteNbr(5550);
request.setRxFillId(null);
// call the custom validator method and verify the response
assertEquals(false, itemAvailableRequestValidator.isValid(request, context));
}
/**
* Test to verify response when SiteNbr is invalid
* @param kioskId it depicts kiosk id
* @param siteNbr it depicts site nbr
* @throws Exception it throws exception
*/
@ParameterizedTest
@CsvSource({"1,5550,true","1,0,false","0,5550,false"})
public void itemsAvailableValidatorTest(int kioskId, int siteNbr,Boolean expected) throws Exception {
ItemAvailableRequest request = new ItemAvailableRequest();
ItemAvailableRequestValidator itemAvailableRequestValidator = new ItemAvailableRequestValidator();
context = Mockito.mock(ConstraintValidatorContext.class);
builder = Mockito.mock(ConstraintValidatorContext.ConstraintViolationBuilder.class);
Mockito.when(context.buildConstraintViolationWithTemplate(Mockito.anyString()))
.thenReturn(builder);
Mockito.when(context.buildConstraintViolationWithTemplate(Mockito.anyString()).addConstraintViolation())
.thenReturn(context);
// set ndc and famId
List<String> ndcs = new ArrayList<>();
List<Integer> famIds = new ArrayList<>();
ndcs.add("1234");
famIds.add(1234);
// set goodRequest fields
request.setNdc(ndcs);
request.setMdsFamId(famIds);
request.setKioskId(kioskId);
request.setQty(1);
request.setSiteNbr(siteNbr);
request.setRxFillId(null); // call the custom validator method and verify the response
assertEquals(expected, itemAvailableRequestValidator.isValid(request, context));
}
/*
test to verify that if rxFillId is 0 then request is not valid
*/
@Test
public void itemsAvailableValidatorFillIdZeroFailureTest() throws Exception {
ItemAvailableRequest request = new ItemAvailableRequest();
// set ndc and famId
List<String> ndcs = new ArrayList<>();
List<Integer> famIds = new ArrayList<>();
ndcs.add("1234");
famIds.add(1234);
// set goodRequest fields
request.setNdc(ndcs);
request.setMdsFamId(famIds);
request.setKioskId(1);
request.setQty(1);
request.setSiteNbr(5550);
request.setRxFillId(0);
// call the custom validator method and verify the response
assertEquals(false, itemAvailableRequestValidator.isValid(request, context));
}
/*
test to verify the valid request with ndc null and required famId
*/
@Test
public void ItemsAvailableValidatorNullNdcSuccessTest() throws Exception {
ItemAvailableRequest request = new ItemAvailableRequest();
// set ndc and famId
List<String> ndcs = null;
List<Integer> famIds = new ArrayList<>();
famIds.add(1234);
// set fields
request.setNdc(ndcs);
request.setMdsFamId(famIds);
request.setQty(1);
request.setSiteNbr(5550);
request.setKioskId(5550);
// call the custom validator method and verify the response
assertTrue(itemAvailableRequestValidator.isValid(request, context));
}
/*
test to verify the fail request with params to be null
*/
@Test
public void itemsAvailableValidatorNullFailureTest() throws Exception {
ItemAvailableRequest request = new ItemAvailableRequest();
// set ndc and famId
List<String> ndcs = null;
List<Integer> famIds = null;
// set goodRequest fields
request.setNdc(ndcs);
request.setMdsFamId(famIds);
// call the custom validator method and verify the response
assertEquals(false, itemAvailableRequestValidator.isValid(request, context));
}
/*
test to verify the valid request with empty ndc and valid famId
*/
@Test
public void itemsAvailableValidatorEmptyNdcSuccessTest() throws Exception {
ItemAvailableRequest request = new ItemAvailableRequest();
// set ndc and famId
List<String> ndcs = new ArrayList<>();
List<Integer> famIds = new ArrayList<>();
famIds.add(1234);
request.setQty(1);
request.setSiteNbr(5550);
request.setKioskId(5550);
// set goodRequest fields
request.setNdc(ndcs);
request.setMdsFamId(famIds);
// call the custom validator method and verify the response
assertTrue(itemAvailableRequestValidator.isValid(request, context));
}
/*
test to verify the fail request with all params to be empty
*/
@Test
public void ItemsAvailableValidatorEmptyFailureTest() throws Exception {
ItemAvailableRequest request = new ItemAvailableRequest();
// set ndc gpi and famId
List<String> ndcs = new ArrayList<>();
List<Integer> famIds = new ArrayList<>();
// set goodRequest fields
request.setNdc(ndcs);
request.setMdsFamId(famIds);
// call the custom validator method and verify the response
assertEquals(false, itemAvailableRequestValidator.isValid(request, context));
}
}
You are mixing JUnit 4
org.junit.Before;
,org.junit.Test
and JUnit 5org.junit.jupiter.*
. This is causing the problems that you have.@ParameterizedTest
is from JUnit 5. So I suggest to use@BeforeEach
and@BeforeAll
andorg.junit.jupiter.api.Test
for@Test
. Basically, drop all JUnit imports that are NOTorg.junit.jupiter.*
including theorg.junit.Assert.*
.Here is one example that works with JUnit 5 and it uses
@BeforeAll
,@ParameterizedTest
,@MethodSource
, and@Test
Class being tested: