Skip to content

Remove mention of @TestComponent from the documentation as it is an implementation detail of @TestConfiguration #8421

@michael-simons

Description

@michael-simons

Hi,

hopefully I'm not doing anything wrong here. I assume that @TestComponent should be picked up when running tests with @SpringBootTest without further ado, but neither this

package com.example;

import org.springframework.boot.test.context.TestComponent;

@TestComponent
public class ToplevelTestComponent {
    
}

together with

package com.example;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

@RunWith(SpringRunner.class)
@SpringBootTest
public class ToplevelTestComponentNotWorkingTest {
    @Autowired
    ToplevelTestComponent toplevelTestComponent;
    
    @Test
    public void f() {
    }
}

nor this (nested version)

package com.example;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.context.TestComponent;
import org.springframework.test.context.junit4.SpringRunner;

@RunWith(SpringRunner.class)
@SpringBootTest
public class NestedTestComponentNotWorkingTest {
    @TestComponent
    static class NestedTestComponent{
    }
    
    @Autowired
    NestedTestComponent nestedTestComponent;
    
    @Test
    public void f() {
    }
}

works. Both fail with org.springframework.beans.factory.NoSuchBeanDefinitionException.
Sample project is attached:
InitializrSpringbootProject.zip
Tested with 1.4.0, 1.4.3 and 1.5.1.

Maybe it's a documentation issue, but I don't see my error…

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions