Skip to content

Context caching across packages is prevented by using @AutoConfigure… on a test class #9282

@wilkinsona

Description

@wilkinsona

Context caching is not performed when two identically configured test classes are in different packages. For example:

package com.example.foo;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

@RunWith(SpringRunner.class)
@SpringBootTest
@AutoConfigureMockMvc
public class FooTests {

	@Test
	public void contextLoads() {
	}

}
package com.example.bar;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

@RunWith(SpringRunner.class)
@SpringBootTest
@AutoConfigureMockMvc
public class BarTests {

	@Test
	public void contextLoads() {
	}

}

The context will be loaded twice due to AutoConfigurationPackages$Registrar returning a PackageImport for the test class from determineImports(AnnotationMetadata). The test classes are in different packages (com.example.foo and com.example.bar) so this is enough to prevent the cache keys from matching.

The problem was introduced in 1.5 as part of the changes made in fa6a138 for #7953.

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions