2

I'm doing a special junit test, that the params are introduced in the front-end of the application by the user and received in back-end of the application. And i want to generate junit test that use that information as parameters.

I saw some guide (like mykong guide and tutorial points) but most of them use static parametrized and i want some dynamic thing. I already tried to use junit annotations, do a set or pass the params to the junit class, use mockito methods but nothings work as dynamic process

Can someone point me to the right direction?

Right now i have something like that

public void run (Object foo)  //Class that contains the information introduced by the user
JUnitCore junit1 = new JUnitCore();
Result result4 = JUnitCore.runClasses(GeneratedTest.getClass()); //Junit class

 //I tried: do a setFoo on the GeneratedTest ; pass the foo on the constructor; 

for (Failure failure : result4.getFailures()) {
                    System.out.println(failure.toString());
}
5
  • Dynamic tests can be constructed with "Test Factories" - dzone.com/articles/… Commented Aug 25, 2017 at 9:48
  • 4
    The behaviour you are searching for is not what a unit test is intended for Commented Aug 25, 2017 at 9:50
  • I'm also thinking that you might misuse unit tests for a wrong purpose. Can you explain us your exact use case? Commented Aug 25, 2017 at 9:54
  • 1
    You want to have integration tests - end-to-end. It's a task for jmeter. In unit-tests you are supposed to test particular components. Say, you check that front-end respects all the settings and doesn't have hard code - outputs on the URL expected data, makes expected requests to BE. Then you check that back-end reacts on the requests and responds properly. When I read your question I also thougth about ReflectionUtils to inject classes. Commented Aug 25, 2017 at 9:55
  • You could set up a special framework like Jenkins or just provide your testing code as normal endpoints (you can still use assertions to make the code 'test-like'). Commented Aug 25, 2017 at 11:03

1 Answer 1

2

Probably not the nicest solution, but maybe an acceptable workaround:

  • generate your unit test so that it fetches parameters from System properties
  • run the generated JUnit test in its own JVM, and pass the parameters/properties on the command line
Sign up to request clarification or add additional context in comments.

1 Comment

This is how it would be done in an automated environment with maven: stackoverflow.com/questions/32120909/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.