我正在研究一个简单的java程序的基本测试驱动开发学习,该程序为股票提供投资组合价值。
我有2类,Portfolio.java
PortfolioTest.java
是一个类,我试图通过使用Mockito模拟这个StockService来为投资组合的功能编写单元测试。
我能够使用已弃用的MockitoAnnotations. initMocks(this);
Stock.java
public class Stock {
private String name;
private int quantity;
public Stock(String name, int quantity) {
this.name = name;
this.quantity = quantity;
}
public String getName() { return name; }
public float getQuantity() { return quantity; }
}
Portfolio.java
import java.util.List;
public class Portfolio {
private List<Stock> stocks;
private StockService stockService;
private Float portfolioValue;
public Portfolio(List<Stock> stocks, Float portfolioValue) {
this.stocks = stocks;
this.portfolioValue = portfolioValue;
}
public void setStockService(StockService stockService) { this.stockService = stockService; }
public Float calculateMarketValue() {
Float marketValue = 0.0f;
for(Stock stock: this.stocks) {
marketValue += (stock.getQuantity()*stockService.getRealtimePrice(stock.getName()));
}
return marketValue;
}
public Boolean isInProfit() {
return (portfolioValue<calculateMarketValue()?true:false);
}
}
StockService.java
public interface StockService {
public float getRealtimePrice(String name);
}
pom. xml
<project>
<dependencies>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.5.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-runner</artifactId>
<version>1.5.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>3.5.13</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>${maven.compiler.source}</source>
<target>${maven.compiler.target}</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.0</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.2.0</version>
<configuration>
<finalName>mockito-basic</finalName>
</configuration>
</plugin>
</plugins>
</build>
</project>
PortfolioTest.java
//@RunWith(MockitoJUnitRunner.class)
public class PortfolioTestMockAnnotations {
//@Rule public MockitoRule rule = MockitoJUnit.rule();
@Mock
StockService stockService;
@InjectMocks
Portfolio portfolio;
@BeforeAll
static void setUp() {
}
@BeforeEach
void init(){
MockitoAnnotations.initMocks(this);
System.out.println(stockService);
when(stockService.getRealtimePrice("infosys")).thenReturn(2200.0f);
when(stockService.getRealtimePrice("reliance")).thenReturn(3100.0f);
when(stockService.getRealtimePrice("indiamart")).thenReturn(4000.0f);
List<Stock> stocks = new ArrayList<>();
stocks.add(new Stock("infosys",10));
stocks.add(new Stock("reliance", 5));
portfolio = new Portfolio(stocks, 35000.0f);
portfolio.setStockService(stockService);
}
@Test
public void calculateMarketValueTest() {
Assertions.assertEquals(portfolio.calculateMarketValue(),37500);
}
@Test
public void calculateIsInProfitTest() {
Assertions.assertTrue(portfolio.isInProfit());
}
}
请建议正确使用@Law的方式
您的POM显示您可能会以某种方式混合JUnit5和JUnit4。有两个注释可以将方法声明为测试:
@org.junit.Test // for JUnit4
@org.junit.jupiter.api.Test // for JUnit5
如果您为JUnit4注释并将test作为JUnit4 test运行,一切都应该没问题。
但是,如果您使用JUnit5进行注释并作为JUnit5测试运行,情况会有所不同。JUnit5不使用@Law
或@RunWith
,但有自己的方式来初始化模拟。这篇文章可能会解释为什么某些东西有效而某些东西无效。
简而言之,要作为JUnit5运行,您的类应该有注释:
@ExtendWith(MockitoExtension.class)
因为@Law
或RunWith
不再起作用。这个注释需要依赖:
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-junit-jupiter</artifactId>
<version>2.23.0</version>
<scope>test</scope>
</dependency>
我只运行JUnit5并且有这些依赖项
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>5.5.2</version>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-junit-jupiter</artifactId>
<version>2.23.0</version>
<scope>test</scope>
</dependency>
相关问题
在Junit5中,@RunsWith()被替换为@Exententwith({MockitoExtension. class}),MockitoExnency来自哪里
<!-- https://mvnrepository.com/artifact/org.mockito/mockito-junit-jupiter -->
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-junit-jupiter</artifactId>
<version>3.11.2</version>
<scope>test</scope>
</dependency>
3.11.2是我发布解决方案时的当前版本。有关更多信息,您可以访问:Mockito和Junit5