小编And*_*ndy的帖子

用post构造测试spring bean

我有一个类似于这个的bean:

@Service
public class A {

    @Autowired
    private B b;

    @PostConstruct
    public void setup() {
       b.call(param);
    }
}

@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = { Application.class, Config.class })
@WebIntegrationTest(randomPort = true)
public class Test {

    @Autowired
    B b;

    @Before
    public void setUp() throws Exception {
        when(b.call(any())).thenReturn("smth");
    }

    @Test
    public void test() throws Exception {
        // test...
    }
}
Run Code Online (Sandbox Code Playgroud)

问题是在测试运行PostConstruct之前调用setUp.

junit spring postconstruct

8
推荐指数
1
解决办法
2万
查看次数

标签 统计

junit ×1

postconstruct ×1

spring ×1