Win a copy of TensorFlow 2.0 in Action this week in the Artificial Intelligence and Machine Learning forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Paul Clapham
  • Bear Bibeault
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Jj Roberts
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • salvin francis
  • Scott Selikoff
  • fred rosenberger

Multiple tests in JUnit

 
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have written a test class to exercise/test another class. Here is my set up code...
public static void main(String args[])
{
junit.textui.TestRunner.run(suite());
}
public static Test suite()
{
return new TestSuite(TestMediaTools.class);
}
public void runTest()
{
setUp();
testCreateCategory();
testCreateMediaType();
testDeleteCategory();
testDeleteMediaType();
tearDown();
}
Ok, now here is the problem... It seems that each one of the above tests get ran 4 times. To make it worse the first one is ran 4 times then the second one 4 times etc...
The problem is, I am getting SQLExceptions thrown from trying to insert a duplicate key.
I cant see why the tests would run 4 times apiece. What am I doing wrong?
Thanks
 
Sheriff
Posts: 9099
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I suggest you take setUp() and tearDown() out of runTest()
 
Bill White
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tried that, didn't work though. Thanks
 
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,
please take out your public void runTest(); method altogether. Junit will call all the methods starting with test...... automatically.
cheers,
friso
 
Ranch Hand
Posts: 775
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Billjr May:
I have written a test class to exercise/test another class. Here is my set up code...

The problem is, I am getting SQLExceptions thrown from trying to insert a duplicate key.


This is actually a separate problem from how you are executing the test. "Good practice" with tests is to ensure that they can be executed at will. In order to have that, any test should establish its test environment before execution. This can be a bit fussy with database materials, but I can tell you what will happen if you don't make the extra effort... you stop running the tests. Something people don't always pick up on with JUnit is that setUp and tearDown are executed before and after each test method. That means you can execute SQL statements to reset the data to a known state before running your individual test methods.
 
Would you turn that thing down? I'm controlling a mind here! Look ... look at the tiny ad ...
the value of filler advertising in 2020
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic