I have a service implemented using Jersey that write/read from a JSON file and I have a test file extending JerseyTest class for testing that service.

What I am trying to do is when running the test it write/read in a different file (with '\_test' in name). That way I don't have the values changed from the main file.

The file is being instanced in the @PostConstruct method like that:

```java
@PostConstruct
public void init() {
    try {
        jsonFile = new NewsJson(); // Need to have a different behavior for testing
    } catch (IOException ioe) {
        LOGGER.error("Error creating file: " + ioe.getMessage());
    }
}

```

What I have tried is to call a @POST method that modify the jsonFile variable, activating writting in a different file. The problem in that solution is the @PostConstruct method being called every request.

How can I change that behavior? Is there a different annotation for initializing services using Jersey?
['https://Stackoverflow.com/questions/30438899', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/2249074/']
**1. HomeController.cs**

```
public class HomeController : Controller
{
    MVCDbContext _db = new MVCDbContext();
    public ActionResult Index()
    {
        return View(_db.Model1.ToList());
    }

    [HttpGet]
    public ActionResult add(Model1 objmo,int?id)
    {
        if (id == null)
        {
            return View();
        }
        else
        {
            Model1 objdata = _db.Model1.Find(id);
            objmo.fname = objdata.fname;
            objmo.lname = objdata.lname;
            return View(objdata);
        }
    }
    [HttpPost]
    public ActionResult add(Model1 obj)
    {
        if (obj.Aref == 0)
        {
            _db.Model1.Add(obj);
        }
        else
        {
            Model1 objdata = _db.Model1.Where(x => x.Aref == obj.Aref).FirstOrDefault();
            objdata.fname = obj.fname;
            objdata.lname = obj.lname;
        }
        _db.SaveChanges();
        return RedirectToAction("Index");
    }
    public ActionResult delete(int? id)
    {
        _db.Model1.Remove(_db.Model1.Find(id));
        _db.SaveChanges();
        return RedirectToAction("Index");
    }
}

```

```html
2. Index.cshtml

@model IEnumerable<mvc_crud_dropdown.Models.Model1>

@{
    ViewBag.Title = "Index";
}

<p>
    @Html.ActionLink("Create New", "add")
</p>
<table>
    <tr>
        <th>
            @Html.DisplayNameFor(model => model.Aref)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.fname)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.lname)
        </th>
        <th></th>
    </tr>

@foreach (var item in Model) {
    <tr>
        <td>
            @Html.DisplayFor(modelItem => item.Aref)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.fname)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.lname)
        </td>
        <td>
            @Html.ActionLink("Edit", "add", new { id=item.Aref  }) |
            @Html.ActionLink("Delete", "Delete", new { id = item.Aref })
        </td>
    </tr>
}

</table>
```
Read this article : [How to Implement Insert, Update, Delete Functionality in Single View of ASP.Net MVC?](http://www.c-sharpcorner.com/UploadFile/bd8b0b/how-to-implement-insert-update-delete-functionality-in-singl/)