NullPointerException when creating tempfiles in rspec #4433

synth opened this Issue Jan 10, 2017 · 4 comments


None yet

2 participants

synth commented Jan 10, 2017 edited

JRuby throws a NullPointerException in Rspec tests when creating tempfiles. The issue occurs when uploading fixture files using the same file across multiple describe blocks.

Here is the test code that exhibits the issue:

require 'spec_helper'

describe 'Tempfile' do
  let(:file) { fixture_file_upload(Rails.root.join('spec/fixtures/file_1.txt').to_s) }

  describe 'test' do
    describe '1' do
      it { expect{file}.to_not raise_exception }

    describe '2' do
      # Test will pass if before block is uncommented
      # Or if config.order = "random" is commented in spec_helper
      # before { ActionMailer::Base.deliveries.clear }
      it { expect{file}.to_not raise_exception }



I've created a sample app here
I've also got multiple builds being tested on TravisCI here:

As you can see, some versions of MRI and JRuby are passing and some aren't. In particular, JRuby v9.1.5.0 is where I first encountered this bug. While this test also doesn't pass in MRI, its in JRuby that I experience the NullPointerException which is why I'm reporting it here. (I think MRI is failing due to some other dependency issue or something...)

As noted in the README of the sample app, the bug appears when two conditions are true:

  1. Rspec.config.order = "random"
  2. ActionMailer::Base.deliveries is not cleared in a before block

Very weird, I know. I would bet those things are somehow triggering something else under the covers though.

Let me know if I can provide more information or if I should go about creating a better test case.


kares commented Jan 10, 2017

confirmed on master, the shortened backtrace points to Tempfile :

  1) Tempfile test 2 
     Failure/Error: it { expect{file}.to_not raise_exception }
       expected no Exception, got java.lang.NullPointerException with backtrace:
         # org.jruby.ext.tempfile.Tempfile$
         # org.jruby.runtime.CallBlock19.doYield(
         # org.jruby.runtime.BlockBody.yield(
         # org.jruby.runtime.Block.yieldValues(
@kares kares added this to the JRuby milestone Jan 10, 2017
@kares kares self-assigned this Jan 10, 2017
kares commented Jan 10, 2017

do have a fix that resolves the NPE and everything seems fine. for whatever reason I'm failing to extract a reproducable isolated test-case. maybe it has to do with rspec's patching, will likely close this without a test.

@kares kares closed this in 886ec64 Jan 10, 2017
synth commented Jan 10, 2017

Thanks for closing this out so fast! Any advice on what to do in the meanwhile until the release is cut?

synth commented Jan 12, 2017

I see was cut yesterday, disregard, thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment