1

I am working on a project with more people and branches. There is a test, written by someone else, which weirdly passes in some of the environments and fails in some other ones.

We are working on separate branches but we made sure that we see the same version of the project with

git pull origin develop

The code looks like:

@Test(expected = RandomCustomException.class)
 public void saveReturnsWithCustomException() throws 
   RandomCustomException {
   xService.save(notValidX);   
}

In some environments it throws a NullPointerException, thus fails, in some other ones it throws a RandomCustomException.

We've checked it, and it's weird, but all the related codes seem to be exactly the same in all the environments.

My xService looks like:

  public X saveX(X x) throws RandomCustomException {
if (!validXName(X.getName())) {
  throw new RandomCustomException("The given name wasn't correct or the field is empty!");
}
return xRepository.save(x);

xRepository is mocked in the test.

xRepository:

public interface xRepository extends JpaRepository<X, Long> {
  X findByApplicationUser(ApplicationUser applicationUser);
}

Does anyone have a suggestion what can the problem be?

lyancsie
  • 658
  • 7
  • 18
  • 3
    In xService is the parameter to saveX() 'X x' guarantied to not be null? – D.Tomov Jan 31 '19 at 09:32
  • It feels like when you do X.getName() X is null when it throws NullPointerException, do you have something like spring profile that exists on some and not other environments? – justMe Jan 31 '19 at 09:35
  • @D.Tomov Not, and yeah, the solution was to change the condition in the service to (x.getName == null || !validXName(X.getName())), but I still can't get how it can be null in my environment and not null in someone else's. For all I know, we haven't done anything that would affect anything from here. Anyhow, it works, so thanks for your help! – lyancsie Jan 31 '19 at 09:53
  • The first problem is that you should debug that NPE. As in: where does it come from? And then, look at the object that is null, and see why it is null. Right now, I am inclined to close this as DUP to https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it+ ... because there is ZERO information here that would enable us to help you. An object is null, so figure out WHICH object that is, and work from there. We can't help with that. – GhostCat Jan 31 '19 at 10:02

1 Answers1

1

The problem is that in this method the x variable is not gurantied to be Not Null so it probably is giving the NullPointerException when you call the method getName.

  public X saveX(X x) throws RandomCustomException {
if (!validXName(X.getName())) {
  throw new RandomCustomException("The given name wasn't correct or the field is empty!");
}
return xRepository.save(x);

I can only speculate the cause might be if 'x' is a bean. It might not meet all the requirements in this environment to instantiate. You can try turning the logs to debug is look for it.

D.Tomov
  • 1,018
  • 2
  • 15
  • 36