NullReferenceException with nested parameterized Tests
See original GitHub issueI’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:
- Created 6 years ago
- Comments:7
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
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
The way we create parametric tests in my team is:
create a kind of blueprint/template of actual test code, with all
context
s andbefore
s,it
s, etc. in place, where the only holes are actual data, say for examplestring scenario, int limit, double expected
.Then we define locally some
TestDataPoint
class that’s just a tuple holding the needed data values:move the test code blueprint into some
buildContext(TestDataPoint point)
method. And iterate over some data set in order to generate tests.(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