I need help with a little experiment. I have two test files:
testFileA.java
and testFileB.java
testFileA.java
looks like
@Test
public void test1() throws Throwable {
Instant start = Instant.now();
mvc.perform(MockMvcRequestBuilders
.post('someEndpoint/')
.andDo(print())
.andExpect(status().isCreated())
Instant end = Instant.now();
System.out.println(">>>>>>>>>>>>>>>>>End" + Duration.between(start, end));
}
@Test
public void test2() throws Throwable {
//same contents as test 1
}
@Test
public void test3() throws Throwable {
//same contents as test 1
}
testFileB.java
looks like
@Test
public void test1() throws Throwable {
Instant start = Instant.now();
this.abstractedCode();
Instant end = Instant.now();
System.out.println(">>>>>>>>>>>>>>>>>End" + Duration.between(start, end));
}
@Test
public void test2() throws Throwable {
//same contents as test 1
}
@Test
public void test3() throws Throwable {
//same contents as test 1
}
public void abstractedCode(){
mvc.perform(MockMvcRequestBuilders
.post('someEndpoint/')
.andDo(print())
.andExpect(status().isCreated())
}
My expectation with testFileB.java
is that the first test would take a little longer and then each subsequent test wouldn't take as long because the duplicated code is refactored into a method call; the Java JIT compiler makes it so we wouldn't have to recompile the same code again. My expectation was proven correct.
My expectation with testFileA.java
is that each test would take as long as the previous one because the common code wasn't refactored into a function, but that ended up not being true. The behavior was the same, the first test took a little longer and then the next two were shorter.
I'm guessing that something is happening within the JVM that makes it so the mvc.perform()
method that I'm calling doesn't have to get recompiled over and over again, the JIT compiler is helping my program out despite the fact that it's not in a function I've defined. Is that true?