betamaxteam / betamax

Betamax is a tool for mocking external HTTP resources such as web services and REST APIs in your tests. The project was inspired by the VCR library for Ruby.
http://betamax.software
Apache License 2.0
468 stars 129 forks source link

NullPointerException when trying to write to tape. #221

Closed ayuk23 closed 7 years ago

ayuk23 commented 7 years ago

My pom includes this dependency:

<dependency>
  <groupId>software.betamax</groupId>
  <artifactId>betamax-junit</artifactId>
  <version>2.0.1</version>
  <scope>test</scope>
</dependency>

and my tests has the following lines of code:

File f = new File("test/resources/betamax/tapes");
Configuration configuration = Configuration.builder().tapeRoot(f).ignoreLocalhost(false).build();
@Rule public RecorderRule recorder = new RecorderRule(configuration);

@Test
@Betamax(tape="my tape")
public void myTest() { ... }

(Note I'm using the Configuration class instead of ProxyConfiguration because there doesn't seem to be a ProxyConfiguration class like in the example like in the examples in this repo)

From the console output, the ProxyServer clearly starts and intercepts my request because I get this warning:
Dec 15, 2016 11:23:39 PM software.betamax.proxy.BetamaxFilters onRequestIntercepted WARNING: no matching request found on my tape

However I get a SocketTimeoutException which shuts down the proxy server and I end up with a null pointer exception:

java.lang.NullPointerException
    at com.myproject.myTest...
...
    at software.betamax.junit.RecorderRule$1.evaluate(RecorderRule.java:75)
    at org.junit.rules.RunRules.evaluate(RunRules.java:20)

Am I missing something obvious here? Thanks in advance for the help.

cowboygneox commented 7 years ago

Doesn't look like you're missing something obvious. Are you using https at all?

ayuk23 commented 7 years ago

@cowboygneox Yes it is definitely making http calls in my test method.

cowboygneox commented 7 years ago

http or https? SSL?

ayuk23 commented 7 years ago

http

cowboygneox commented 7 years ago

What http client?

cowboygneox commented 7 years ago

Are you sure that your http client is correctly proxying to Betamax?

ayuk23 commented 7 years ago
        HttpClientDefinition definition = HttpClientDefinition.builder()
                .serviceName("service")
                .url(url)
                .connectionTimeout(Duration.parse("PT2S"))
                .readTimeout(Duration.parse("PT2S"))
                .writeTimeout(Duration.parse("PT2S"))
                .build();

        this.client = OkHttpClientConfig.build(definition, new MetricRegistry());

        Request request = new Request.Builder().url(url).build();

How can I check that it is correctly proxying? I first get software.betamax.handler.NonWritableTapeException: Tape is not writable then my program disconnects the proxy to server connection and I get a SocketTimeoutException and then finally a NullPOinterException. If it is the case where I am not correctly proxying to Betamax, how could I fix this? I thought all that was required was to include the betamax annotation.

cowboygneox commented 7 years ago

If you're getting non-writeable tape, that's a good thing, because it means the tape isn't writeable :).

You have to change the TAPE_MODE in the @Betamax notation. You only rarely want to commit a test with writeable tests; usually you want to write once, and then fix the test to stop writing. Try changing the TAPE_MODE.

ayuk23 commented 7 years ago

Awesome. That definitely worked! Just a heads up though, the documentation at http://betamax.software says that READ_WRITE is the default tape mode, but it doesn't seem like this is the case since not setting mode=TapeMode.READ_WRITE doesn't seem to work.

Thanks again for your help!

cowboygneox commented 7 years ago

That documentation is super old now, but I'll leave this open until I make an actionable item on how to make that more intuitive. I thought about just having local.properties that allow developers to write to it, but then CI won't. I'll probably move in a direction like that.

cowboygneox commented 7 years ago

Moving this to our Documentation Trello Card: https://trello.com/c/9jdBEGaR