在 Spring 中,在对象上运行一堆测试方法但在每次新测试方法调用之前重置/重新注入对象的最佳方法是什么?我尝试在下面的代码中执行此操作,但使用我当前的逻辑,该对象仅被创建和注入一次。
package com.bidtracker;
import org.junit.Test;
import org.junit.runner.RunWith;
import static org.junit.Assert.*;
import static org.hamcrest.CoreMatchers.is;
import com.bidtracker.iface.BidTracker;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration
public class BidTrackerTest {
@Autowired
BidTracker tracker;
@Test
public void shouldReturnHighestBidAmount1(){
tracker.bidOnItem("itemB", "user1", 105);
assertThat(tracker.getHighestBid("itemB").getAmount(),is(Integer.valueOf(105)));
}
@Test
public void shouldReturnHighestBidAmount2(){
tracker.bidOnItem("itemB", "user1", 39);
tracker.bidOnItem("itemB", "user1", 50);
assertThat(tracker.getHighestBid("itemB").getAmount(),is(Integer.valueOf(50)));
}
}