question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

NullReferenceException with nested parameterized Tests

See original GitHub issue

I’m working with parameterized complex tests (simplified version below). When using a second parameterized test the 'sub’testvalues are passed through from the top level testcase. Which results in a NullPointer regarding my toplevel dataManager.

I already tried to call my testvalue-method directly. This does not work as well. Only instanciating my test object inline seems to work (but not usable because of too much code)

How does building tests influence the object instanciation / execution of before,after steps?

void before_all()
{
    dataManager = new DataManager(system);
    dataManager.Setup();
}

void after_all()
{
    dataManager.Cleanup();
}

void one_test()
{
    new Each<string, RecurrentTestParams3>
    {
        { "Description", new RecurrentTestParams3("Param Description", () => dataManager.GetInputDataArray(), () => ValidateData()) }
    }.Do((configName, data) =>
    {
        context["{0} with {1} on {2}".With(configName, data.Name, system)] = () => doCommonTest(configName, data);
    });
}

private void doCommonTest(string configName, RecurrentTestParams3 datas)
{
    it["Basic Check"] = () =>
    {
        sample.ShouldNotBeNull();
    };

    context["Then Process"] = () =>
    {
        new Each<string, MyDataType>
        {
            //{ "One", datas.InputData()[0]}, --original
            //{ "Two", datas.InputData()[1]}, --original
            //{ "Three", datas.InputData()[2]} --original
            //{ "One", dataManager.getData1()}, --modified
            //{ "Two", dataManager.getData2()}, --modified
            //{ "Three", dataManager.getData3()} --modified
            { "One", new MyDataType() }, -- works
            { "Two", new MyDataType()}, -- works
            { "Three", new MyDataType()} -- works
        }.Do((dataName, data) =>
        {
            it["Work {0}".With(dataName)] = () => operationResult = sample.DoWork(data);
        });

        context["Then Validate"] = () =>
        {
            beforeAll = () => results = dataManager.QueryData();

            data.Validator();
        };
    };
}

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:7

github_iconTop GitHub Comments

1reaction
r-weissercommented, Jul 4, 2017

Yes, I guess I’m just digging into it and this seems to be the best approach. I wasn’t aware of this way of functionality and that I mixed testdata with data needed to build test context.

Regards

0reactions
BrainCrumbzcommented, Jul 4, 2017

The way we create parametric tests in my team is:

  1. create a kind of blueprint/template of actual test code, with all contexts and befores, its, etc. in place, where the only holes are actual data, say for example string scenario, int limit, double expected.

    context[$"given {scenario}"] = () =>
    {
      before = () =>
      {
        // ...
        someMock.Limit = limit;
        // ...
      };
      
      it[$"returns {expected}"] = () =>
      {
        // ...
        actual.Should().Be(expected);
      };
    }
    
  2. Then we define locally some TestDataPoint class that’s just a tuple holding the needed data values:

    class TestDataPoint
    {
      string scenario;
      int limit;
      double expected;
    
      // maybe, a costructor to set values
    }
    
  3. move the test code blueprint into some buildContext(TestDataPoint point) method. And iterate over some data set in order to generate tests.

    new []
    {
      new TestDataPoint(/* ... */),
      new TestDataPoint(/* ... */),
      new TestDataPoint(/* ... */),
    
    }.Do(buildContext);
    

(please forgive any syntax error, that was just typed in here)

Less moving parts between test-build and test-run time, just data generated and passed along. HTH

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to pass a null value in JUnit 5 parameterized tests
1. Solution – @NullSource. Since JUnit 5.4 and above, we can use the @NullSource to pass a null value to the parameterized tests....
Read more >
Cannot create a @ParameterizedTest ...
Cant create a nested parameterized test that uses @MethodSource . @MethodSource requires that it points to a static method.
Read more >
Beyond the simple: An in-depth look at JUnit 5's nested ...
Parameterized tests constitute a frequently used and appreciated feature, regardless of the testing framework. Here's some good news: The JUnit ...
Read more >
how to access a parent variable from a inner nested test in ...
Your code works on my computer as expected. Foo is null since every test is dealt a new instance of the inner and...
Read more >
JUnit Tutorial: Writing Nested Tests In JUnit 5
This blog on JUnit 5 nested tests discusses the JUnit 5 feature - @Nested annotation, which allows creating an inner class within the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found