Results 1 to 4 of 4
  1. #1
    trucall is offline Member
    Join Date
    Feb 2014
    Posts
    2
    Rep Power
    0

    Default Junit Test behavior

    Hi,

    I've this test:

    Java Code:
    ...
    @FixMethodOrder(MethodSorters.NAME_ASCENDING)
    public class TestTest {
    
        private String testString;
    
        @Test
        public void test00() throws Exception {
            Assert.assertNull(this.testString);
            this.testString = "Spain";
            Assert.assertTrue("Spain".equals(this.testString));
        }
    
        @Test
        public void test01() throws Exception {
            Assert.assertNotNull(this.testString);
        }
    }
    I don't understand the behavior.
    I would expect testString equal to "Spain".
    Why "testString" is null in "test01"?


    Thanks
    Tru

  2. #2
    gimbal2 is online now Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,159
    Rep Power
    6

    Default Re: Junit Test behavior

    You are making one test depend on another; don't do that. Tests should be completely isolated from each other.

    I believe the 'problem' is that JUnit will create a new instance of the class for each test it invokes, so testString is reset to null as soon as test01 runs so you can have a nice clean setup without having to do manual cleanup.

    So the real solution is to use JUnit properly: don't make one test depend on the output of another.
    A test that you can do to prove that I am in fact correct is to make testString a static variable; then it should keep the value set by test00.
    Last edited by gimbal2; 02-08-2014 at 01:35 PM.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  3. #3
    trucall is offline Member
    Join Date
    Feb 2014
    Posts
    2
    Rep Power
    0

    Default Re: Junit Test behavior

    I understand this logic but looks at this test scenary:

    login()

    getUsers()

    getRoles()

    ...

    I thought the cleanest solution was populate a cookiestore object on first test, all test methods could use same session cookie.


    Thanks!
    Tru

  4. #4
    notivago is offline Heavy Coffe Drinker
    Join Date
    Feb 2014
    Location
    São Paulo, Brazil
    Posts
    29
    Rep Power
    0

    Default Re: Junit Test behavior

    I will start by telling you to give your tests meaningful names. As gimbal2 stated, junit guarantees that each test is run as if it was the only test and it is unaffected by the others. In this case your testString has the default field value for objects, which is null. Thus every time a test method is invoked it will be with testString == null.

    You have @Before to prepare data for each test, so use it to assign to the test string.

    Lastly, on your last "example" you should mock the cookies and provide a controled environment for each method test.

Similar Threads

  1. JUnit test and ArrayIndexOutOfBoundsException
    By Hazza in forum New To Java
    Replies: 17
    Last Post: 02-22-2013, 03:21 PM
  2. junit test problem
    By moamen in forum Eclipse
    Replies: 2
    Last Post: 03-14-2010, 10:41 PM
  3. Replies: 1
    Last Post: 08-03-2009, 06:31 PM
  4. JUnit Test Help!
    By pharo in forum New To Java
    Replies: 0
    Last Post: 04-10-2009, 06:15 PM
  5. Junit test
    By alice in forum New To Java
    Replies: 1
    Last Post: 06-14-2008, 02:24 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •