JUnit test fails with NPE when testing validator method


Sudir

I am working through the following tutorial:

http://docs.spring.io/docs/Spring-MVC-step-by-step/part4.html

I am trying to test the validate method in the class below:

public class PriceIncreaseValidator implements Validator {
    public void validate(Object obj, Errors errors) {
        PriceIncrease pi = (PriceIncrease) obj;
        if (pi == null) {
            errors.rejectValue("percentage", "error.not-specified", null, "Value required.");
        }
........
}

Below is my JUnit test case:

private PriceIncreaseValidator priceIncreaseValidator;
private PriceIncrease priceIncrease;
private Errors errors;

public void testEmptyPriceIncrease() {

    priceIncreaseValidator.validate(priceIncrease, errors); // This is where I get NPE

    assertTrue(errors.hasErrors());
}

So when calling the validate method in my JUnit test case, I get NPE. Both tests and classes compile.

I've tried converting priceIncrease to an object as follows, but still no luck:

Object obj = priceIncrease;
//validate
priceIncreaseValidator.validate(obj, errors);

Any ideas?

As you can see, I'm trying to test if the price increase is not set, which returns an error when calling validate.

Kevin Bowersox

No fields are PriceIncreaseValidator,PriceIncrease,Errorsinstantiated .

Create instances of these fields, or use Spring to autowire them.

To create an Errorsobject, use:

Errors errors = new BeanPropertyBindingResult(priceIncrease, "priceIncrease");

complete example

public void testEmptyPriceIncrease() {
    PriceIncreaseValidator priceIncreaseValidator = new PriceIncreaseValidator();
    PriceIncrease priceIncrease = new PriceIncrease();
    Errors errors = new BeanPropertyBindingResult(priceIncrease, "priceIncrease");

    priceIncreaseValidator.validate(priceIncrease, errors); // This is where I get NPE

    assertTrue(errors.hasErrors());
}

Related


JUnit test fails with NPE when testing validator method

Sudir I am going through the following tutorial: http://docs.spring.io/docs/Spring-MVC-step-by-step/part4.html I am trying to test the validate method in the class below: public class PriceIncreaseValidator implements Validator { public void validate(Objec

JUnit test fails with NPE when testing validator method

Sudir I am working through the following tutorial: http://docs.spring.io/docs/Spring-MVC-step-by-step/part4.html I am trying to test the validate method in the class below: public class PriceIncreaseValidator implements Validator { public void validate(Obj

JUnit test failing with NPE when testing validator method

Sudhir I am working through the below tutorial: http://docs.spring.io/docs/Spring-MVC-step-by-step/part4.html I am trying to test the validate method in the class below: public class PriceIncreaseValidator implements Validator { public void validate(Object

JUnit test fails when annotating method arg @Nonnull

Freddy Boucher: Can someone explain why my JUnit tests are failing: notNullMethodTest() using @NotNull -> success nonnullMethodTest() with @Nonnull - > fails Unexpected exception, expected <java.lang.NullPointerException>, but got <java.lang.IllegalArgumentExc

JUnit test fails when annotating method arg @Nonnull

Freddy Boucher: Can someone explain why my JUnit tests are failing: notNullMethodTest() using @NotNull -> success nonnullMethodTest() with @Nonnull - > fails Unexpected exception, expected <java.lang.NullPointerException>, but got <java.lang.IllegalArgumentExc

Run a method if the test fails on Junit

Keva 161 I've added a method to my Selenium framework that takes a screenshot at the end of a test, but it doesn't run all the way to the end because some tests fail. I want to implement a rule to run this screenshot method if the test fails. It's just like: @

JUnit testing with multiple test cases in one method

Pankajic I have the following class A with a constructor that takes two strings as parameters. Class A { String test1; String test2; public(String test1, String test2) { this.test1 = test1; this.test2 = test2; } } I want to test a constru

JUnit's LinkedList method test fails, why?

UserFuser I have this method which will search a LinkedList (named ListNode) and check for chars, and check if they contain uppercase chars, then store it in a new linked list, and return it. I wrote code for this and tested it with JUnit and it failed JUNit (

JUnit's LinkedList method test fails, why?

UserFuser I have this method which will search a LinkedList (named ListNode) and check for chars, and check if they contain uppercase chars, then store it in a new linked list, and return it. I wrote code for this and tested it with JUnit and it failed JUNit (

NPE when unit testing

Mara 122 I am trying to write unit tests for my method getBestSellers(). here is: package bookstore.scraper.book.scrapingtypeservice; import bookstore.scraper.enums.Bookstore; import bookstore.scraper.book.Book; import bookstore.scraper.fetcher.empik.EmpikFet

NPE when unit testing

Mara 122 I am trying to write unit tests for my method getBestSellers(). here is: package bookstore.scraper.book.scrapingtypeservice; import bookstore.scraper.enums.Bookstore; import bookstore.scraper.book.Book; import bookstore.scraper.fetcher.empik.EmpikFet

Test fails on BlocListener when testing widget

Mihai Neagoe I have two tests that check if the snack bar is displayed when streaming and handling the login state using a BlocListener. void main() async { AuthenticationRepositoryMock _authenticationRepositoryMock; LoginBlocMock _loginBloc; final fireB

Test fails when testing room insertion with Rx

coroutine scheduler I'm trying to do a simple CRUD test on my single entity, but I can't figure out what I'm doing wrong, so the test always ends in AssertionError. Here is my database setup: @Database(entities = [Habit::class], version = 1) @TypeConverters(Co

Test fails when testing room insertion with Rx

coroutine scheduler I'm trying to do a simple CRUD test on my single entity, but I can't figure out what I'm doing wrong, so the test always ends in AssertionError. Here is my database setup: @Database(entities = [Habit::class], version = 1) @TypeConverters(Co

Test fails on BlocListener when testing widget

Mihai Neagoe I have two tests that check if the snack bar is displayed when streaming and handling the login state using a BlocListener. void main() async { AuthenticationRepositoryMock _authenticationRepositoryMock; LoginBlocMock _loginBloc; final fireB

Test fails on BlocListener when testing widget

Mihai Neagoe I have two tests that check if the snack bar is displayed when streaming and handling the login state using a BlocListener. void main() async { AuthenticationRepositoryMock _authenticationRepositoryMock; LoginBlocMock _loginBloc; final fireB

It fails when testing with 90 test cases

Arouin I am making a simple program to check if a UID is valid: It must contain at least 2 uppercase English alphabetic characters. It must contain at least 3 numbers (0-9). It should only contain alphanumeric characters (az, AZ and 0-9). No role should be rep

Test fails on BlocListener when testing widget

Mihai Neagoe I have two tests that check if the snack bar is displayed when streaming and handling the login state using a BlocListener. void main() async { AuthenticationRepositoryMock _authenticationRepositoryMock; LoginBlocMock _loginBloc; final fireB

Test fails on BlocListener when testing widget

Mihai Neagoe I have two tests that check if the snack bar is displayed when streaming and handling the login state using a BlocListener. void main() async { AuthenticationRepositoryMock _authenticationRepositoryMock; LoginBlocMock _loginBloc; final fireB

No such method error when creating JUnit test

IaCoder: I've tried the problem for the past two days with no luck. I just want to create an annotation based JUnit test using spring framework and hibernate. My IDE is netbeans 6.5 and I am using hibernate 3, spring 2.5.5 and JUnit 4.4. This is the error I ge

No such method error when creating JUnit test

IaCoder: I've tried the problem for the past two days with no luck. I just want to create an annotation based JUnit test using spring framework and hibernate. My IDE is netbeans 6.5 and I am using hibernate 3, spring 2.5.5 and JUnit 4.4. This is the error I ge

junit test fails with exception

Sebastian Dietrich I am using junit to test my Metrics class. When the test runs, I don't get any exceptions, but the test doesn't succeed. I've tried to find all the different questions but haven't found any. Here is its code. public class MetricsTest { pri

jUnit test on JAVA fails

k2wln I'm new here and sorry for the stupid question. I have some problems with my JUnit test: for some reason it shows me an error in index.html. I attach the link to my repository through tests and screens. Thanks! mistake: org.opentest4j.AssertionFailedErro