0

So I have junit tests, let's pretend this is my code:

@Test
public void test1() {
  Object o = new Object("123");
  AssertTrue(o.string == "123");
}

@Test
public void test2() {
  Object o = new Object("456");
  AssertTrue(o.string == "456");
}

Let's pretend the code has a public string field that appends an argument to that string. Now test1 returns true, because it's "123", but test2 returns false because it's "123456", which means Object o still exists when test 2 executes. I didn't have any problems doing tests in Junit before, but now my object's string just accumulates for each test.

Aren't objects supposed to be automatically destroyed when a java method finishes execution? Can I force an object to be deleted?

Thanks.

Edited by iamsmooth: n/a

1
Contributor
1
Reply
2
Views
7 Years
Discussion Span
Last Post by iamsmooth
0

I removed the keyword "static" from everything in this object's class and it no longer fails. However, I don't understand why this makes a difference. Shouldn't java destroy things not being used? I also have the main method non-static, which doesn't really make sense to me...

Can anyone explain to me what's happening?

Thanks :D

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.