|
| 1 | +package unit.server.model; |
| 2 | + |
| 3 | +import com.arm.nimbus.collab.server.model.PersistentEntity; |
| 4 | +import com.arm.nimbus.collab.server.model.Product; |
| 5 | +import org.junit.*; |
| 6 | +import org.junit.runner.RunWith; |
| 7 | +import org.mockito.Mockito; |
| 8 | +import org.powermock.api.mockito.PowerMockito; |
| 9 | +import org.powermock.core.classloader.annotations.PrepareForTest; |
| 10 | +import org.powermock.modules.junit4.PowerMockRunner; |
| 11 | + |
| 12 | +/** |
| 13 | + * TODO description |
| 14 | + * |
| 15 | + * @creator victor |
| 16 | + */ |
| 17 | +@RunWith(PowerMockRunner.class) |
| 18 | +@PrepareForTest(Product.class) |
| 19 | +public class ProductUnitTest{ |
| 20 | + |
| 21 | + Product p; |
| 22 | + |
| 23 | + @Before |
| 24 | + public void setUp() throws Exception { |
| 25 | + p = new Product(); |
| 26 | + } |
| 27 | + |
| 28 | + @After |
| 29 | + public void tearDown() throws Exception { |
| 30 | + } |
| 31 | + |
| 32 | + @Test |
| 33 | + public void name() throws Exception { |
| 34 | + String name = "mike"; |
| 35 | + p.setName(name); |
| 36 | + Assert.assertEquals(name, p.getName()); |
| 37 | + } |
| 38 | + |
| 39 | + @Test |
| 40 | + public void code() throws Exception { |
| 41 | + String code = "c32p56"; |
| 42 | + p.setCode(code); |
| 43 | + Assert.assertEquals(code, p.getCode()); |
| 44 | + } |
| 45 | + |
| 46 | + @Test |
| 47 | + public void isPersistentEntity(){ |
| 48 | + PersistentEntity entity = p; |
| 49 | + Assert.assertNotNull(entity.getId()); |
| 50 | + Assert.assertNull(entity.getVersion()); |
| 51 | + } |
| 52 | + |
| 53 | + @Test |
| 54 | + /* |
| 55 | + * Mock a static method. |
| 56 | + * Really new way of testing your java. |
| 57 | + * @doc http://code.google.com/p/powermock/wiki/MockitoUsage |
| 58 | + */ |
| 59 | + |
| 60 | + public void findByID(){ |
| 61 | + PowerMockito.mockStatic(Product.class); |
| 62 | + Mockito.when(Product.findById("id")).thenReturn(new Product()); |
| 63 | + |
| 64 | + Product r = Product.findById("id"); |
| 65 | + Assert.assertEquals(p.getCode(), r.getCode()); |
| 66 | + Assert.assertEquals(p.getName(), r.getName()); |
| 67 | + } |
| 68 | +} |
0 commit comments